<commit_before>from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import sys
import pandas
import logging
from ccdproc import ImageFileCollection
from ..core import get_twilight_time, ra_dec_to_deg
from ..core import NightDataContainer

# log = logging.getLogger(__name__)

class NightOrganizer(object):

    def __init__(self, full_path, instrument, technique, ignore_bias=False,
                 ignore_flats=False):
        """Initializes the NightOrganizer class

        This class contains methods to organize the data for processing. It will
        identify groups of OBJECTS, FLATS or COMPS (comparison lamps) whenever
        they exist. The product will be an object that will act as a data
        container.

        Args:
            full_path (str): Full path to data location
            instrument (str): Instrument name, it refers to the camera used,
              for pipeline it can be Blue or Red
            technique (str): Technique used to obtain the data, Spectroscopy or
              Imaging.
            ignore_bias (bool): Flag that allows to bypass the processing of
              Bias.
            ignore_flats (bool): Flag that allows to bypass the processing of
              Flats.

        """
        self.log = logging.getLogger(__name__)
        self.path = full_path
        self.instrument = instrument
        self.technique = technique
        self.ignore_bias = ignore_bias
        self.ignore_flats = ignore_flats
        self.keywords = ['date',
                         'slit',
                         'date-obs',
                         'obstype',
                         'object',
                         'exptime',
                         'obsra',
                         'obsdec',
                         'grating',
                         'cam_targ',
                         'grt_targ',
                         'filter',
                         'filter2',
                         'gain',
                         'rdnoise',
                         'roi']
        self.file_collection = None
        self.all_datatypes = None

        self.data_container = NightDataContainer(path=self.path,
                                                 instrument=self.instrument,
                                                 technique=self.technique)

        self.day_time_data = None
        self.night_time_data = None

    def __call__(self):
        """Call method

        Creates a table with selected keywords that will allow to group the data
        in order to be classified according to the observational technique used,
        imaging or spectroscopy.

        Returns:
            data_container_list (list): List of instances of NightDataContainer
            which is the class used as storage unit for classified data. The
            number of elements depends on the number of combinations of `GAIN`,
            `RDNOISE` and `ROI`.

        """

        ifc = ImageFileCollection(self.path, self.keywords)
        self.file_collection = ifc.summary.to_pandas()
        # if technique is Spectroscopy, ignore all data that has
        # WAVMODE = Imaging because assumes they are acquisition images
        if self.technique == 'Spectroscopy':
            self.log.warning("Ignoring all Imaging data. Assuming they are "
                             "acquisition exposures")
            self.file_collection = self.file_collection[
                self.file_collection.wavmode != 'Imaging']
        elif self.technique == 'Imaging':
            self.log.warning("Ignoring all files where `wavmode` is not "
                             "Imaging.")
            self.file_collection = self.file_collection[
                self.file_collection.wavmode == 'Imaging']

        # add two columns that will contain the ra and dec in degrees

        self.file_collection['radeg'] = ''
        self.file_collection['decdeg'] = ''
        for i in self.file_collection.index.tolist():

            radeg, decdeg = ra_dec_to_deg(self.file_collection.obsra.iloc[i],
                                          self.file_collection.obsdec.iloc[i])

            self.file_collection.iloc[
                i, self.file_collection.columns.get_loc('radeg')] = \
                '{:.2f}'.format(radeg)

            self.file_collection.iloc[
                i, self.file_collection.columns.get_loc('decdeg')] = \
                '{:.2f}'.format(decdeg)
            # now we can compare using degrees

        readout_configurations = self.file_collection.groupby(
            ['gain',
             'rdnoise',
             'roi']).size().reset_index().rename(columns={0: 'count'})

        data_container_list = []
        for i in readout_configurations.index:
            if not self.data_container.is_empty:
                print("Reset data container")
                self.data_container.reset()

            sub_collection = self.file_collection[
                ((self.file_collection['gain'] ==
                  self.file_collection.iloc[i]['gain']) &
                 (self.file_collection['rdnoise'] ==
                  self.file_collection.iloc[i]['rdnoise']) &
                 (self.file_collection['roi'] ==
                  self.file_collection.iloc[i]['roi']))]

            self.all_datatypes = sub_collection.obstype.unique()
            if self.technique == 'Spectroscopy':
                self.spectroscopy_night(file_collection=sub_collection,
                                        data_container=self.data_container)
            elif self.technique == 'Imaging':
                self.imaging_night()

            if self.data_container.is_empty:
                self.log.debug('data_container is empty')
                data_container_list.append(None)
                # sys.exit('ERROR: There is no data to process!')
            else:
                self.log.debug('Appending classified data')
                data_container_list.append(self.data_container)

        # Warn the user in case the list of data_container element is empty or
        # all the elements are None
        if len(data_container_list) == 0 or not all(data_container_list):
            self.log.warning("It is possible that there is no valid data.")

        return data_container_list

    def spectroscopy_night(self, file_collection, data_container):
        """Organizes data for spectroscopy

        This method identifies all combinations of nine **key** keywords that
        can set appart different objects with their respective calibration data
        or not. The keywords used are: GAIN, RDNOISE, GRATING, FILTER2,
        CAM_TARG,GRT_TARG, SLIT, OBSRA and OBSDEC.

        This method populates the `data_container` class attribute which is an
        instance of the class NightDataContainer.
        A data group is an instance of a Pandas DataFrame.

        """

        assert isinstance(file_collection, pandas.DataFrame)
        assert isinstance(data_container, NightDataContainer)

        # obtain a list of timestamps of observing time
        # this will only be used for naming flats
        dateobs_list = file_collection['date-obs'].tolist()

        # get times for twilights, sunset an sunrise
        afternoon_twilight, morning_twilight, sun_set, sun_rise = \
            get_twilight_time(date_obs=dateobs_list)

        # set times in data container
        data_container.set_sun_times(sun_set,
                                     sun_rise)
        data_container.set_twilight_times(afternoon_twilight,
                                          morning_twilight)

        # process bias
        bias_collection = file_collection[file_collection.obstype == 'BIAS']

        if not self.ignore_bias:
            if len(bias_collection) == 0:
                self.log.critical('There is no BIAS images. Use --ignore-bias'
                                  'to continue without BIAS.')
                sys.exit('CRITICAL ERROR: BIAS not Found.')
            else:
                bias_conf = bias_collection.groupby(
                    ['gain',
                     'rdnoise',
                     'radeg',
                     'decdeg']).size().reset_index().rename(
                    columns={0: 'count'})

                # bias_conf
                for i in bias_conf.index:
                    bias_group = bias_collection[(
                        (bias_collection['gain'] == bias_conf.iloc[i]['gain']) &
                        (bias_collection['rdnoise'] == bias_conf.iloc[i][
                            'rdnoise']) &
                        (bias_collection['radeg'] == bias_conf.iloc[i][
                            'radeg']) &
                        (bias_collection['decdeg'] == bias_conf.iloc[i][
                            'decdeg']))]

                    data_container.add_bias(bias_group=bias_group)
        else:
            self.log.warning('Ignoring BIAS by request.')

        if 'FLAT' not in file_collection.obstype.unique() and \
                not self.ignore_flats:
            self.log.critical('There is no FLAT images. Use --ignore-flats to '
                              'continue without FLATs.')
            sys.exit('CRITICAL ERROR: FLAT not Found.')
        elif self.ignore_flats:
            self.log.warning('Ignoring FLAT images on request.')
            data_collection = file_collection[
                ((file_collection.obstype != 'BIAS') &
                 (file_collection.obstype != 'FLAT'))]
        else:
            # process non-bias i.e. flats and object ... and comp
            data_collection = file_collection[file_collection.obstype != 'BIAS']

        confs = data_collection.groupby(
            ['gain',
             'rdnoise',
             'grating',
             'filter2',
             'cam_targ',
             'grt_targ',
             'slit',
             'radeg',
             'decdeg']).size().reset_index().rename(columns={0: 'count'})

        for i in confs.index:

            data_group = data_collection[(
                (data_collection['gain'] == confs.iloc[i]['gain']) &
                (data_collection['rdnoise'] == confs.iloc[i]['rdnoise']) &
                (data_collection['grating'] == confs.iloc[i]['grating']) &
                (data_collection['filter2'] == confs.iloc[i]['filter2']) &
                (data_collection['cam_targ'] == confs.iloc[i]['cam_targ']) &
                (data_collection['grt_targ'] == confs.iloc[i]['grt_targ']) &
                (data_collection['slit'] == confs.iloc[i]['slit']) &
                (data_collection['radeg'] == confs.iloc[i]['radeg']) &
                (data_collection['decdeg'] == confs.iloc[i]['decdeg']))]

            group_obstype = data_group.obstype.unique()

            if 'FLAT' in group_obstype and len(group_obstype) == 1:

                data_container.add_day_flats(data_group)

            else:
                # Comparison lamps are processed as science data.
                data_container.add_data_group(data_group)

                if 'FLAT' in group_obstype:
                    # grab flats and put them in the flats group as well
                    object_flat_group = data_group[
                        data_group['obstype'] == 'FLAT']
                    data_container.add_day_flats(object_flat_group)

        return data_container

    def imaging_night(self):
        """Organizes data for imaging

        For imaging there is no discrimination regarding night data since the
        process is simpler. It is a three stage process classifying BIAS, FLAT
        and OBJECT datatype. The data is packed in groups that are
        pandas.DataFrame objects.

        """

        # bias data group
        date_obs_list = self.file_collection['date-obs'].tolist()

        afternoon_twilight, morning_twilight, sun_set, sun_rise = \
            get_twilight_time(date_obs=date_obs_list)

        self.data_container.set_sun_times(sun_set=sun_set,
                                          sun_rise=sun_rise)

        self.data_container.set_twilight_times(evening=afternoon_twilight,
                                               morning=morning_twilight)

        bias_group = self.file_collection[
            self.file_collection.obstype == 'BIAS']  # .tolist()

        if len(bias_group) > 2:

            bias_confs = bias_group.groupby(
                ['gain',
                 'rdnoise',
                 'radeg',
                 'decdeg']).size().reset_index().rename(columns={0: 'count'})

            for i in bias_confs.index:

                bias_group = bias_group[(
                    (bias_group['gain'] == bias_confs.iloc[i]['gain']) &
                    (bias_group['rdnoise'] == bias_confs.iloc[i]['rdnoise']) &
                    (bias_group['radeg'] == bias_confs.iloc[i]['radeg']) &
                    (bias_group['decdeg'] == bias_confs.iloc[i]['decdeg']))]

                self.data_container.add_bias(bias_group)
        else:
            self.log.error('Not enough bias images.')

        # flats separation
        flat_data = self.file_collection[self.file_collection.obstype == 'FLAT']

        # confs stands for configurations
        confs = flat_data.groupby(
            ['object',
             'filter']).size().reset_index().rename(columns={0: 'count'})

        for i in confs.index:

            flat_group = flat_data[
                ((flat_data['object'] == confs.iloc[i]['object']) &
                 (flat_data['filter'] == confs.iloc[i]['filter']))]

            self.data_container.add_day_flats(flat_group)

        # science data separation
        science_data = self.file_collection[
            self.file_collection.obstype == 'OBJECT']

        # confs stands for configurations
        confs = science_data.groupby(
            ['object',
             'filter']).size().reset_index().rename(columns={0: 'count'})

        for i in confs.index:

            science_group = science_data[
                ((science_data['object'] == confs.iloc[i]['object']) &
                 (science_data['filter'] == confs.iloc[i]['filter']))]

            self.data_container.add_data_group(science_group)
<commit_msg>added wavmode to list of keywords used.<commit_after>from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import sys
import pandas
import logging
from ccdproc import ImageFileCollection
from ..core import get_twilight_time, ra_dec_to_deg
from ..core import NightDataContainer

# log = logging.getLogger(__name__)

class NightOrganizer(object):

    def __init__(self, full_path, instrument, technique, ignore_bias=False,
                 ignore_flats=False):
        """Initializes the NightOrganizer class

        This class contains methods to organize the data for processing. It will
        identify groups of OBJECTS, FLATS or COMPS (comparison lamps) whenever
        they exist. The product will be an object that will act as a data
        container.

        Args:
            full_path (str): Full path to data location
            instrument (str): Instrument name, it refers to the camera used,
              for pipeline it can be Blue or Red
            technique (str): Technique used to obtain the data, Spectroscopy or
              Imaging.
            ignore_bias (bool): Flag that allows to bypass the processing of
              Bias.
            ignore_flats (bool): Flag that allows to bypass the processing of
              Flats.

        """
        self.log = logging.getLogger(__name__)
        self.path = full_path
        self.instrument = instrument
        self.technique = technique
        self.ignore_bias = ignore_bias
        self.ignore_flats = ignore_flats
        self.keywords = ['date',
                         'slit',
                         'date-obs',
                         'obstype',
                         'object',
                         'exptime',
                         'obsra',
                         'obsdec',
                         'grating',
                         'cam_targ',
                         'grt_targ',
                         'filter',
                         'filter2',
                         'gain',
                         'rdnoise',
                         'roi',
                         'wavmode']
        self.file_collection = None
        self.all_datatypes = None

        self.data_container = NightDataContainer(path=self.path,
                                                 instrument=self.instrument,
                                                 technique=self.technique)

        self.day_time_data = None
        self.night_time_data = None

    def __call__(self):
        """Call method

        Creates a table with selected keywords that will allow to group the data
        in order to be classified according to the observational technique used,
        imaging or spectroscopy.

        Returns:
            data_container_list (list): List of instances of NightDataContainer
            which is the class used as storage unit for classified data. The
            number of elements depends on the number of combinations of `GAIN`,
            `RDNOISE` and `ROI`.

        """

        ifc = ImageFileCollection(self.path, self.keywords)
        self.file_collection = ifc.summary.to_pandas()
        # if technique is Spectroscopy, ignore all data that has
        # WAVMODE = Imaging because assumes they are acquisition images
        if self.technique == 'Spectroscopy':
            self.log.warning("Ignoring all Imaging data. Assuming they are "
                             "acquisition exposures")
            self.file_collection = self.file_collection[
                self.file_collection.wavmode != 'Imaging']
        elif self.technique == 'Imaging':
            self.log.warning("Ignoring all files where `wavmode` is not "
                             "Imaging.")
            self.file_collection = self.file_collection[
                self.file_collection.wavmode == 'Imaging']

        # add two columns that will contain the ra and dec in degrees

        self.file_collection['radeg'] = ''
        self.file_collection['decdeg'] = ''
        for i in self.file_collection.index.tolist():

            radeg, decdeg = ra_dec_to_deg(self.file_collection.obsra.iloc[i],
                                          self.file_collection.obsdec.iloc[i])

            self.file_collection.iloc[
                i, self.file_collection.columns.get_loc('radeg')] = \
                '{:.2f}'.format(radeg)

            self.file_collection.iloc[
                i, self.file_collection.columns.get_loc('decdeg')] = \
                '{:.2f}'.format(decdeg)
            # now we can compare using degrees

        readout_configurations = self.file_collection.groupby(
            ['gain',
             'rdnoise',
             'roi']).size().reset_index().rename(columns={0: 'count'})

        data_container_list = []
        for i in readout_configurations.index:
            if not self.data_container.is_empty:
                print("Reset data container")
                self.data_container.reset()

            sub_collection = self.file_collection[
                ((self.file_collection['gain'] ==
                  self.file_collection.iloc[i]['gain']) &
                 (self.file_collection['rdnoise'] ==
                  self.file_collection.iloc[i]['rdnoise']) &
                 (self.file_collection['roi'] ==
                  self.file_collection.iloc[i]['roi']))]

            self.all_datatypes = sub_collection.obstype.unique()
            if self.technique == 'Spectroscopy':
                self.spectroscopy_night(file_collection=sub_collection,
                                        data_container=self.data_container)
            elif self.technique == 'Imaging':
                self.imaging_night()

            if self.data_container.is_empty:
                self.log.debug('data_container is empty')
                data_container_list.append(None)
                # sys.exit('ERROR: There is no data to process!')
            else:
                self.log.debug('Appending classified data')
                data_container_list.append(self.data_container)

        # Warn the user in case the list of data_container element is empty or
        # all the elements are None
        if len(data_container_list) == 0 or not all(data_container_list):
            self.log.warning("It is possible that there is no valid data.")

        return data_container_list

    def spectroscopy_night(self, file_collection, data_container):
        """Organizes data for spectroscopy

        This method identifies all combinations of nine **key** keywords that
        can set appart different objects with their respective calibration data
        or not. The keywords used are: GAIN, RDNOISE, GRATING, FILTER2,
        CAM_TARG,GRT_TARG, SLIT, OBSRA and OBSDEC.

        This method populates the `data_container` class attribute which is an
        instance of the class NightDataContainer.
        A data group is an instance of a Pandas DataFrame.

        """

        assert isinstance(file_collection, pandas.DataFrame)
        assert isinstance(data_container, NightDataContainer)

        # obtain a list of timestamps of observing time
        # this will only be used for naming flats
        dateobs_list = file_collection['date-obs'].tolist()

        # get times for twilights, sunset an sunrise
        afternoon_twilight, morning_twilight, sun_set, sun_rise = \
            get_twilight_time(date_obs=dateobs_list)

        # set times in data container
        data_container.set_sun_times(sun_set,
                                     sun_rise)
        data_container.set_twilight_times(afternoon_twilight,
                                          morning_twilight)

        # process bias
        bias_collection = file_collection[file_collection.obstype == 'BIAS']

        if not self.ignore_bias:
            if len(bias_collection) == 0:
                self.log.critical('There is no BIAS images. Use --ignore-bias'
                                  'to continue without BIAS.')
                sys.exit('CRITICAL ERROR: BIAS not Found.')
            else:
                bias_conf = bias_collection.groupby(
                    ['gain',
                     'rdnoise',
                     'radeg',
                     'decdeg']).size().reset_index().rename(
                    columns={0: 'count'})

                # bias_conf
                for i in bias_conf.index:
                    bias_group = bias_collection[(
                        (bias_collection['gain'] == bias_conf.iloc[i]['gain']) &
                        (bias_collection['rdnoise'] == bias_conf.iloc[i][
                            'rdnoise']) &
                        (bias_collection['radeg'] == bias_conf.iloc[i][
                            'radeg']) &
                        (bias_collection['decdeg'] == bias_conf.iloc[i][
                            'decdeg']))]

                    data_container.add_bias(bias_group=bias_group)
        else:
            self.log.warning('Ignoring BIAS by request.')

        if 'FLAT' not in file_collection.obstype.unique() and \
                not self.ignore_flats:
            self.log.critical('There is no FLAT images. Use --ignore-flats to '
                              'continue without FLATs.')
            sys.exit('CRITICAL ERROR: FLAT not Found.')
        elif self.ignore_flats:
            self.log.warning('Ignoring FLAT images on request.')
            data_collection = file_collection[
                ((file_collection.obstype != 'BIAS') &
                 (file_collection.obstype != 'FLAT'))]
        else:
            # process non-bias i.e. flats and object ... and comp
            data_collection = file_collection[file_collection.obstype != 'BIAS']

        confs = data_collection.groupby(
            ['gain',
             'rdnoise',
             'grating',
             'filter2',
             'cam_targ',
             'grt_targ',
             'slit',
             'radeg',
             'decdeg']).size().reset_index().rename(columns={0: 'count'})

        for i in confs.index:

            data_group = data_collection[(
                (data_collection['gain'] == confs.iloc[i]['gain']) &
                (data_collection['rdnoise'] == confs.iloc[i]['rdnoise']) &
                (data_collection['grating'] == confs.iloc[i]['grating']) &
                (data_collection['filter2'] == confs.iloc[i]['filter2']) &
                (data_collection['cam_targ'] == confs.iloc[i]['cam_targ']) &
                (data_collection['grt_targ'] == confs.iloc[i]['grt_targ']) &
                (data_collection['slit'] == confs.iloc[i]['slit']) &
                (data_collection['radeg'] == confs.iloc[i]['radeg']) &
                (data_collection['decdeg'] == confs.iloc[i]['decdeg']))]

            group_obstype = data_group.obstype.unique()

            if 'FLAT' in group_obstype and len(group_obstype) == 1:

                data_container.add_day_flats(data_group)

            else:
                # Comparison lamps are processed as science data.
                data_container.add_data_group(data_group)

                if 'FLAT' in group_obstype:
                    # grab flats and put them in the flats group as well
                    object_flat_group = data_group[
                        data_group['obstype'] == 'FLAT']
                    data_container.add_day_flats(object_flat_group)

        return data_container

    def imaging_night(self):
        """Organizes data for imaging

        For imaging there is no discrimination regarding night data since the
        process is simpler. It is a three stage process classifying BIAS, FLAT
        and OBJECT datatype. The data is packed in groups that are
        pandas.DataFrame objects.

        """

        # bias data group
        date_obs_list = self.file_collection['date-obs'].tolist()

        afternoon_twilight, morning_twilight, sun_set, sun_rise = \
            get_twilight_time(date_obs=date_obs_list)

        self.data_container.set_sun_times(sun_set=sun_set,
                                          sun_rise=sun_rise)

        self.data_container.set_twilight_times(evening=afternoon_twilight,
                                               morning=morning_twilight)

        bias_group = self.file_collection[
            self.file_collection.obstype == 'BIAS']  # .tolist()

        if len(bias_group) > 2:

            bias_confs = bias_group.groupby(
                ['gain',
                 'rdnoise',
                 'radeg',
                 'decdeg']).size().reset_index().rename(columns={0: 'count'})

            for i in bias_confs.index:

                bias_group = bias_group[(
                    (bias_group['gain'] == bias_confs.iloc[i]['gain']) &
                    (bias_group['rdnoise'] == bias_confs.iloc[i]['rdnoise']) &
                    (bias_group['radeg'] == bias_confs.iloc[i]['radeg']) &
                    (bias_group['decdeg'] == bias_confs.iloc[i]['decdeg']))]

                self.data_container.add_bias(bias_group)
        else:
            self.log.error('Not enough bias images.')

        # flats separation
        flat_data = self.file_collection[self.file_collection.obstype == 'FLAT']

        # confs stands for configurations
        confs = flat_data.groupby(
            ['object',
             'filter']).size().reset_index().rename(columns={0: 'count'})

        for i in confs.index:

            flat_group = flat_data[
                ((flat_data['object'] == confs.iloc[i]['object']) &
                 (flat_data['filter'] == confs.iloc[i]['filter']))]

            self.data_container.add_day_flats(flat_group)

        # science data separation
        science_data = self.file_collection[
            self.file_collection.obstype == 'OBJECT']

        # confs stands for configurations
        confs = science_data.groupby(
            ['object',
             'filter']).size().reset_index().rename(columns={0: 'count'})

        for i in confs.index:

            science_group = science_data[
                ((science_data['object'] == confs.iloc[i]['object']) &
                 (science_data['filter'] == confs.iloc[i]['filter']))]

            self.data_container.add_data_group(science_group)
<|endoftext|>


<commit_before>"""Test the Raspberry Pi hardware platform."""
import pytest

from homeassistant.components.hassio import DATA_OS_INFO
from homeassistant.components.raspberry_pi.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from tests.common import MockModule, mock_integration

async def test_hardware_info(hass: HomeAssistant, hass_ws_client) -> None:
    """Test we can get the board info."""
    mock_integration(hass, MockModule("hassio"))
    hass.data[DATA_OS_INFO] = {"board": "rpi"}

    assert await async_setup_component(hass, DOMAIN, {})

    client = await hass_ws_client(hass)

    await client.send_json({"id": 1, "type": "hardware/info"})
    msg = await client.receive_json()

    assert msg["id"] == 1
    assert msg["success"]
    assert msg["result"] == {
        "hardware": [
            {
                "board": {
                    "hassio_board_id": "rpi",
                    "manufacturer": "raspberry_pi",
                    "model": "1",
                    "revision": None,
                },
                "name": "Raspberry Pi",
                "url": None,
            }
        ]
    }

@pytest.mark.parametrize("os_info", [None, {"board": None}, {"board": "other"}])
async def test_hardware_info_fail(hass: HomeAssistant, hass_ws_client, os_info) -> None:
    """Test async_info raises if os_info is not as expected."""
    mock_integration(hass, MockModule("hassio"))
    hass.data[DATA_OS_INFO] = os_info

    assert await async_setup_component(hass, DOMAIN, {})

    client = await hass_ws_client(hass)

    await client.send_json({"id": 1, "type": "hardware/info"})
    msg = await client.receive_json()

    assert msg["id"] == 1
    assert msg["success"]
    assert msg["result"] == {"hardware": []}
<commit_msg>Improve raspberry_pi tests (#72557)<commit_after>"""Test the Raspberry Pi hardware platform."""
from unittest.mock import patch

import pytest

from homeassistant.components.raspberry_pi.const import DOMAIN
from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry, MockModule, mock_integration

async def test_hardware_info(hass: HomeAssistant, hass_ws_client) -> None:
    """Test we can get the board info."""
    mock_integration(hass, MockModule("hassio"))

    # Setup the config entry
    config_entry = MockConfigEntry(
        data={},
        domain=DOMAIN,
        options={},
        title="Raspberry Pi",
    )
    config_entry.add_to_hass(hass)
    with patch(
        "homeassistant.components.raspberry_pi.get_os_info",
        return_value={"board": "rpi"},
    ):
        assert await hass.config_entries.async_setup(config_entry.entry_id)
        await hass.async_block_till_done()

    client = await hass_ws_client(hass)

    with patch(
        "homeassistant.components.raspberry_pi.hardware.get_os_info",
        return_value={"board": "rpi"},
    ):
        await client.send_json({"id": 1, "type": "hardware/info"})
        msg = await client.receive_json()

    assert msg["id"] == 1
    assert msg["success"]
    assert msg["result"] == {
        "hardware": [
            {
                "board": {
                    "hassio_board_id": "rpi",
                    "manufacturer": "raspberry_pi",
                    "model": "1",
                    "revision": None,
                },
                "name": "Raspberry Pi",
                "url": None,
            }
        ]
    }

@pytest.mark.parametrize("os_info", [None, {"board": None}, {"board": "other"}])
async def test_hardware_info_fail(hass: HomeAssistant, hass_ws_client, os_info) -> None:
    """Test async_info raises if os_info is not as expected."""
    mock_integration(hass, MockModule("hassio"))

    # Setup the config entry
    config_entry = MockConfigEntry(
        data={},
        domain=DOMAIN,
        options={},
        title="Raspberry Pi",
    )
    config_entry.add_to_hass(hass)
    with patch(
        "homeassistant.components.raspberry_pi.get_os_info",
        return_value={"board": "rpi"},
    ):
        assert await hass.config_entries.async_setup(config_entry.entry_id)
        await hass.async_block_till_done()

    client = await hass_ws_client(hass)

    with patch(
        "homeassistant.components.raspberry_pi.hardware.get_os_info",
        return_value=os_info,
    ):
        await client.send_json({"id": 1, "type": "hardware/info"})
        msg = await client.receive_json()

    assert msg["id"] == 1
    assert msg["success"]
    assert msg["result"] == {"hardware": []}
<|endoftext|>


<commit_before>import itertools
import unittest

import numpy
import pytest

import cupy
from cupy import testing

float_types = [numpy.float16, numpy.float32, numpy.float64]
complex_types = [numpy.complex, numpy.complex64, numpy.complex128]
signed_int_types = [numpy.int8, numpy.int16, numpy.int32, numpy.int64]
unsigned_int_types = [numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64]
int_types = signed_int_types + unsigned_int_types
all_types = [numpy.bool] + float_types + int_types + complex_types
negative_types = (
    [numpy.bool] + float_types + signed_int_types + complex_types)
negative_no_complex_types = [numpy.bool] + float_types + signed_int_types
no_complex_types = [numpy.bool] + float_types + int_types

@testing.gpu
@testing.parameterize(*(
    testing.product({
        'nargs': [1],
        'name': ['reciprocal', 'conj', 'conjugate', 'angle'],
    }) + testing.product({
        'nargs': [2],
        'name': [
            'add', 'multiply', 'divide', 'power', 'subtract', 'true_divide',
            'floor_divide', 'fmod', 'remainder'],
    })
))
class TestArithmeticRaisesWithNumpyInput(unittest.TestCase):

    def test_raises_with_numpy_input(self):
        nargs = self.nargs
        name = self.name

        # Check TypeError is raised if numpy.ndarray is given as input
        func = getattr(cupy, name)
        for input_xp_list in itertools.product(*[[numpy, cupy]] * nargs):
            if all(xp is cupy for xp in input_xp_list):
                # We don't test all-cupy-array inputs here
                continue
            arys = [xp.array([2, -3]) for xp in input_xp_list]
            with self.assertRaises(TypeError):
                func(*arys)

@testing.gpu
@testing.parameterize(*(
    testing.product({
        'arg1': ([testing.shaped_arange((2, 3), numpy, dtype=d)
                  for d in all_types
                  ] + [0, 0.0j, 0j, 2, 2.0, 2j, True, False]),
        'name': ['conj', 'conjugate', 'angle', 'real', 'imag'],
    }) + testing.product({
        'arg1': ([numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                  for d in negative_types
                  ] + [0, 0.0j, 0j, 2, 2.0, 2j, -2, -2.0, -2j, True, False]),
        'name': ['angle'],
    }) + testing.product({
        'arg1': ([testing.shaped_arange((2, 3), numpy, dtype=d) + 1
                  for d in all_types
                  ] + [2, 2.0, 2j, True]),
        'name': ['reciprocal'],
    })
))
class TestArithmeticUnary(unittest.TestCase):

    @testing.numpy_cupy_allclose(atol=1e-5)
    def test_unary(self, xp):
        arg1 = self.arg1
        if isinstance(arg1, numpy.ndarray):
            arg1 = xp.asarray(arg1)
        y = getattr(xp, self.name)(arg1)

        if self.name in ('real', 'imag'):
            # Some NumPy functions return Python scalars for Python scalar
            # inputs.
            # We need to convert them to arrays to compare with CuPy outputs.
            if xp is numpy and isinstance(arg1, (bool, int, float, complex)):
                y = xp.asarray(y)

            # TODO(niboshi): Fix this
            # numpy.real and numpy.imag return Python int if the input is
            # Python bool. CuPy should return an array of dtype.int32 or
            # dtype.int64 (depending on the platform) in such cases, instead
            # of an array of dtype.bool.
            if xp is cupy and isinstance(arg1, bool):
                y = y.astype(int)

        return y

@testing.parameterize(*testing.product({
    'shape': [(3, 2), (), (3, 0, 2)],
}))
@testing.gpu
class TestComplex(unittest.TestCase):

    @testing.for_all_dtypes(no_complex=True)
    @testing.numpy_cupy_array_equal()
    def test_real_ndarray_nocomplex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        real = x.real
        assert real is x  # real returns self
        return real

    @testing.for_all_dtypes(no_complex=True)
    @testing.numpy_cupy_array_equal()
    def test_real_nocomplex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        real = xp.real(x)
        assert real is x  # real returns self
        return real

    @testing.for_all_dtypes(no_complex=True)
    @testing.numpy_cupy_array_equal()
    def test_imag_ndarray_nocomplex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        imag = x.imag
        return imag

    @testing.for_all_dtypes(no_complex=True)
    @testing.numpy_cupy_array_equal()
    def test_imag_nocomplex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        imag = xp.imag(x)
        return imag

    @testing.for_complex_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_real_ndarray_complex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        x_ = x.copy()
        real = x_.real
        # real returns a view
        assert real.base is x_
        x_ += 1 + 1j
        testing.assert_array_equal(real, x.real + 1)
        return real

    @testing.for_complex_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_real_complex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        x_ = x.copy()
        real = xp.real(x_)
        # real returns a view
        assert real.base is x_
        x_ += 1 + 1j
        testing.assert_array_equal(real, x.real + 1)
        return real

    @testing.for_complex_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_imag_ndarray_complex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        x_ = x.copy()
        imag = x_.imag
        # imag returns a view
        assert imag.base is x_
        x_ += 1 + 1j
        testing.assert_array_equal(imag, x.imag + 1)
        return imag

    @testing.for_complex_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_imag_complex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        x_ = x.copy()
        imag = xp.imag(x_)
        # imag returns a view
        assert imag.base is x_
        x_ += 1 + 1j
        testing.assert_array_equal(imag, x.imag + 1)
        return imag

class ArithmeticBinaryBase:

    @testing.numpy_cupy_allclose(atol=1e-4)
    def check_binary(self, xp):
        arg1 = self.arg1
        arg2 = self.arg2
        np1 = numpy.asarray(arg1)
        np2 = numpy.asarray(arg2)
        dtype1 = np1.dtype
        dtype2 = np2.dtype

        if self.name == 'power':
            # TODO(niboshi): Fix this: power(0, 1j)
            #     numpy => 1+0j
            #     cupy => 0j
            if dtype2 in complex_types and (np1 == 0).any():
                return xp.array(True)

            # TODO(niboshi): Fix this: xp.power(0j, 0)
            #     numpy => 1+0j
            #     cupy => 0j
            c_arg1 = dtype1 in complex_types
            if c_arg1 and (np1 == 0j).any() and (np2 == 0).any():
                return xp.array(True)

        # TODO(niboshi): Fix this: xp.add(0j, xp.array([2.], 'f')).dtype
        #     numpy => complex64
        #     cupy => complex128
        if isinstance(arg1, complex):
            if dtype2 in (numpy.float16, numpy.float32):
                return xp.array(True)

        if isinstance(arg1, numpy.ndarray):
            arg1 = xp.asarray(arg1)
        if isinstance(arg2, numpy.ndarray):
            arg2 = xp.asarray(arg2)

        # Subtraction between booleans is not allowed.
        if (self.name == 'subtract'
                and dtype1 == numpy.bool_
                and dtype2 == numpy.bool_):
            return xp.array(True)

        func = getattr(xp, self.name)
        with testing.NumpyError(divide='ignore'):
            with numpy.warnings.catch_warnings():
                numpy.warnings.filterwarnings('ignore')
                if self.use_dtype:
                    y = func(arg1, arg2, dtype=self.dtype)
                else:
                    y = func(arg1, arg2)

        # TODO(niboshi): Fix this. If rhs is a Python complex,
        #    numpy returns complex64
        #    cupy returns complex128
        if xp is cupy and isinstance(arg2, complex):
            if dtype1 in (numpy.float16, numpy.float32):
                y = y.astype(numpy.complex64)

        # NumPy returns different values (nan/inf) on division by zero
        # depending on the architecture.
        # As it is not possible for CuPy to replicate this behavior, we ignore
        # the difference here.
        if self.name in ('floor_divide', 'remainder'):
            if y.dtype in (float_types + complex_types) and (np2 == 0).any():
                y = xp.asarray(y)
                y[y == numpy.inf] = numpy.nan
                y[y == -numpy.inf] = numpy.nan

        return y

@testing.gpu
@testing.parameterize(*(
    testing.product({
        # TODO(unno): boolean subtract causes DeprecationWarning in numpy>=1.13
        'arg1': [testing.shaped_arange((2, 3), numpy, dtype=d)
                 for d in all_types
                 ] + [0, 0.0, 0j, 2, 2.0, 2j, True, False],
        'arg2': [testing.shaped_reverse_arange((2, 3), numpy, dtype=d)
                 for d in all_types
                 ] + [0, 0.0, 0j, 2, 2.0, 2j, True, False],
        'name': ['add', 'multiply', 'power', 'subtract'],
    }) + testing.product({
        'arg1': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in negative_types
                 ] + [0, 0.0, 0j, 2, 2.0, 2j, -2, -2.0, -2j, True, False],
        'arg2': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in negative_types
                 ] + [0, 0.0, 0j, 2, 2.0, 2j, -2, -2.0, -2j, True, False],
        'name': ['divide', 'true_divide', 'subtract'],
    })
))
class TestArithmeticBinary(ArithmeticBinaryBase, unittest.TestCase):

    def test_binary(self):
        self.use_dtype = False
        self.check_binary()

@testing.gpu
@testing.parameterize(*(
    testing.product({
        'arg1': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in int_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'arg2': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in int_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'name': ['true_divide'],
        'dtype': [numpy.float64],
        'use_dtype': [True, False],
    }) + testing.product({
        'arg1': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in float_types] + [0.0, 2.0, -2.0],
        'arg2': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in float_types] + [0.0, 2.0, -2.0],
        'name': ['power', 'true_divide', 'subtract'],
        'dtype': [numpy.float64],
        'use_dtype': [True, False],
    }) + testing.product({
        'arg1': [testing.shaped_arange((2, 3), numpy, dtype=d)
                 for d in no_complex_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'arg2': [testing.shaped_reverse_arange((2, 3), numpy, dtype=d)
                 for d in no_complex_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'name': ['floor_divide', 'fmod', 'remainder'],
        'dtype': [numpy.float64],
        'use_dtype': [True, False],
    }) + testing.product({
        'arg1': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in negative_no_complex_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'arg2': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in negative_no_complex_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'name': ['floor_divide', 'fmod', 'remainder'],
        'dtype': [numpy.float64],
        'use_dtype': [True, False],
    })
))
class TestArithmeticBinary2(ArithmeticBinaryBase, unittest.TestCase):

    def test_binary(self):
        if (self.use_dtype and
                numpy.lib.NumpyVersion(numpy.__version__) < '1.10.0'):
            raise unittest.SkipTest('Test for numpy>=1.10')
        self.check_binary()

class TestArithmeticModf(unittest.TestCase):

    @testing.for_float_dtypes()
    @testing.numpy_cupy_allclose()
    def test_modf(self, xp, dtype):
        a = xp.array([-2.5, -1.5, -0.5, 0, 0.5, 1.5, 2.5], dtype=dtype)
        b, c = xp.modf(a)
        d = xp.empty((2, 7), dtype=dtype)
        d[0] = b
        d[1] = c
        return d

@testing.parameterize(*testing.product({
    'xp': [numpy, cupy],
    'shape': [(3, 2), (), (3, 0, 2)]
}))
@testing.gpu
class TestBoolSubtract(unittest.TestCase):

    def test_bool_subtract(self):
        xp = self.xp
        if xp is numpy and not testing.numpy_satisfies('>=1.14.0'):
            raise unittest.SkipTest('NumPy<1.14.0')
        shape = self.shape
        x = testing.shaped_random(shape, xp, dtype=numpy.bool_)
        y = testing.shaped_random(shape, xp, dtype=numpy.bool_)
        with pytest.raises(TypeError):
            xp.subtract(x, y)
<commit_msg>Change test for 0 ** 0 check<commit_after>import itertools
import unittest

import numpy
import pytest

import cupy
from cupy import testing

float_types = [numpy.float16, numpy.float32, numpy.float64]
complex_types = [numpy.complex, numpy.complex64, numpy.complex128]
signed_int_types = [numpy.int8, numpy.int16, numpy.int32, numpy.int64]
unsigned_int_types = [numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64]
int_types = signed_int_types + unsigned_int_types
all_types = [numpy.bool] + float_types + int_types + complex_types
negative_types = (
    [numpy.bool] + float_types + signed_int_types + complex_types)
negative_no_complex_types = [numpy.bool] + float_types + signed_int_types
no_complex_types = [numpy.bool] + float_types + int_types

@testing.gpu
@testing.parameterize(*(
    testing.product({
        'nargs': [1],
        'name': ['reciprocal', 'conj', 'conjugate', 'angle'],
    }) + testing.product({
        'nargs': [2],
        'name': [
            'add', 'multiply', 'divide', 'power', 'subtract', 'true_divide',
            'floor_divide', 'fmod', 'remainder'],
    })
))
class TestArithmeticRaisesWithNumpyInput(unittest.TestCase):

    def test_raises_with_numpy_input(self):
        nargs = self.nargs
        name = self.name

        # Check TypeError is raised if numpy.ndarray is given as input
        func = getattr(cupy, name)
        for input_xp_list in itertools.product(*[[numpy, cupy]] * nargs):
            if all(xp is cupy for xp in input_xp_list):
                # We don't test all-cupy-array inputs here
                continue
            arys = [xp.array([2, -3]) for xp in input_xp_list]
            with self.assertRaises(TypeError):
                func(*arys)

@testing.gpu
@testing.parameterize(*(
    testing.product({
        'arg1': ([testing.shaped_arange((2, 3), numpy, dtype=d)
                  for d in all_types
                  ] + [0, 0.0j, 0j, 2, 2.0, 2j, True, False]),
        'name': ['conj', 'conjugate', 'angle', 'real', 'imag'],
    }) + testing.product({
        'arg1': ([numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                  for d in negative_types
                  ] + [0, 0.0j, 0j, 2, 2.0, 2j, -2, -2.0, -2j, True, False]),
        'name': ['angle'],
    }) + testing.product({
        'arg1': ([testing.shaped_arange((2, 3), numpy, dtype=d) + 1
                  for d in all_types
                  ] + [2, 2.0, 2j, True]),
        'name': ['reciprocal'],
    })
))
class TestArithmeticUnary(unittest.TestCase):

    @testing.numpy_cupy_allclose(atol=1e-5)
    def test_unary(self, xp):
        arg1 = self.arg1
        if isinstance(arg1, numpy.ndarray):
            arg1 = xp.asarray(arg1)
        y = getattr(xp, self.name)(arg1)

        if self.name in ('real', 'imag'):
            # Some NumPy functions return Python scalars for Python scalar
            # inputs.
            # We need to convert them to arrays to compare with CuPy outputs.
            if xp is numpy and isinstance(arg1, (bool, int, float, complex)):
                y = xp.asarray(y)

            # TODO(niboshi): Fix this
            # numpy.real and numpy.imag return Python int if the input is
            # Python bool. CuPy should return an array of dtype.int32 or
            # dtype.int64 (depending on the platform) in such cases, instead
            # of an array of dtype.bool.
            if xp is cupy and isinstance(arg1, bool):
                y = y.astype(int)

        return y

@testing.parameterize(*testing.product({
    'shape': [(3, 2), (), (3, 0, 2)],
}))
@testing.gpu
class TestComplex(unittest.TestCase):

    @testing.for_all_dtypes(no_complex=True)
    @testing.numpy_cupy_array_equal()
    def test_real_ndarray_nocomplex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        real = x.real
        assert real is x  # real returns self
        return real

    @testing.for_all_dtypes(no_complex=True)
    @testing.numpy_cupy_array_equal()
    def test_real_nocomplex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        real = xp.real(x)
        assert real is x  # real returns self
        return real

    @testing.for_all_dtypes(no_complex=True)
    @testing.numpy_cupy_array_equal()
    def test_imag_ndarray_nocomplex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        imag = x.imag
        return imag

    @testing.for_all_dtypes(no_complex=True)
    @testing.numpy_cupy_array_equal()
    def test_imag_nocomplex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        imag = xp.imag(x)
        return imag

    @testing.for_complex_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_real_ndarray_complex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        x_ = x.copy()
        real = x_.real
        # real returns a view
        assert real.base is x_
        x_ += 1 + 1j
        testing.assert_array_equal(real, x.real + 1)
        return real

    @testing.for_complex_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_real_complex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        x_ = x.copy()
        real = xp.real(x_)
        # real returns a view
        assert real.base is x_
        x_ += 1 + 1j
        testing.assert_array_equal(real, x.real + 1)
        return real

    @testing.for_complex_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_imag_ndarray_complex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        x_ = x.copy()
        imag = x_.imag
        # imag returns a view
        assert imag.base is x_
        x_ += 1 + 1j
        testing.assert_array_equal(imag, x.imag + 1)
        return imag

    @testing.for_complex_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_imag_complex(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype=dtype)
        x_ = x.copy()
        imag = xp.imag(x_)
        # imag returns a view
        assert imag.base is x_
        x_ += 1 + 1j
        testing.assert_array_equal(imag, x.imag + 1)
        return imag

class ArithmeticBinaryBase:

    @testing.numpy_cupy_allclose(atol=1e-4)
    def check_binary(self, xp):
        arg1 = self.arg1
        arg2 = self.arg2
        np1 = numpy.asarray(arg1)
        np2 = numpy.asarray(arg2)
        dtype1 = np1.dtype
        dtype2 = np2.dtype

        if self.name == 'power':
            # TODO(niboshi): Fix this: power(0, 1j)
            #     numpy => 1+0j
            #     cupy => 0j
            if dtype2 in complex_types and (np1 == 0).any():
                return xp.array(True)

        # TODO(niboshi): Fix this: xp.add(0j, xp.array([2.], 'f')).dtype
        #     numpy => complex64
        #     cupy => complex128
        if isinstance(arg1, complex):
            if dtype2 in (numpy.float16, numpy.float32):
                return xp.array(True)

        if isinstance(arg1, numpy.ndarray):
            arg1 = xp.asarray(arg1)
        if isinstance(arg2, numpy.ndarray):
            arg2 = xp.asarray(arg2)

        # Subtraction between booleans is not allowed.
        if (self.name == 'subtract'
                and dtype1 == numpy.bool_
                and dtype2 == numpy.bool_):
            return xp.array(True)

        func = getattr(xp, self.name)
        with testing.NumpyError(divide='ignore'):
            with numpy.warnings.catch_warnings():
                numpy.warnings.filterwarnings('ignore')
                if self.use_dtype:
                    y = func(arg1, arg2, dtype=self.dtype)
                else:
                    y = func(arg1, arg2)

        # TODO(niboshi): Fix this. If rhs is a Python complex,
        #    numpy returns complex64
        #    cupy returns complex128
        if xp is cupy and isinstance(arg2, complex):
            if dtype1 in (numpy.float16, numpy.float32):
                y = y.astype(numpy.complex64)

        # NumPy returns different values (nan/inf) on division by zero
        # depending on the architecture.
        # As it is not possible for CuPy to replicate this behavior, we ignore
        # the difference here.
        if self.name in ('floor_divide', 'remainder'):
            if y.dtype in (float_types + complex_types) and (np2 == 0).any():
                y = xp.asarray(y)
                y[y == numpy.inf] = numpy.nan
                y[y == -numpy.inf] = numpy.nan

        return y

@testing.gpu
@testing.parameterize(*(
    testing.product({
        # TODO(unno): boolean subtract causes DeprecationWarning in numpy>=1.13
        'arg1': [testing.shaped_arange((2, 3), numpy, dtype=d)
                 for d in all_types
                 ] + [0, 0.0, 0j, 2, 2.0, 2j, True, False],
        'arg2': [testing.shaped_reverse_arange((2, 3), numpy, dtype=d)
                 for d in all_types
                 ] + [0, 0.0, 0j, 2, 2.0, 2j, True, False],
        'name': ['add', 'multiply', 'power', 'subtract'],
    }) + testing.product({
        'arg1': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in negative_types
                 ] + [0, 0.0, 0j, 2, 2.0, 2j, -2, -2.0, -2j, True, False],
        'arg2': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in negative_types
                 ] + [0, 0.0, 0j, 2, 2.0, 2j, -2, -2.0, -2j, True, False],
        'name': ['divide', 'true_divide', 'subtract'],
    })
))
class TestArithmeticBinary(ArithmeticBinaryBase, unittest.TestCase):

    def test_binary(self):
        self.use_dtype = False
        self.check_binary()

@testing.gpu
@testing.parameterize(*(
    testing.product({
        'arg1': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in int_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'arg2': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in int_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'name': ['true_divide'],
        'dtype': [numpy.float64],
        'use_dtype': [True, False],
    }) + testing.product({
        'arg1': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in float_types] + [0.0, 2.0, -2.0],
        'arg2': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in float_types] + [0.0, 2.0, -2.0],
        'name': ['power', 'true_divide', 'subtract'],
        'dtype': [numpy.float64],
        'use_dtype': [True, False],
    }) + testing.product({
        'arg1': [testing.shaped_arange((2, 3), numpy, dtype=d)
                 for d in no_complex_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'arg2': [testing.shaped_reverse_arange((2, 3), numpy, dtype=d)
                 for d in no_complex_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'name': ['floor_divide', 'fmod', 'remainder'],
        'dtype': [numpy.float64],
        'use_dtype': [True, False],
    }) + testing.product({
        'arg1': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in negative_no_complex_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'arg2': [numpy.array([-3, -2, -1, 1, 2, 3], dtype=d)
                 for d in negative_no_complex_types
                 ] + [0, 0.0, 2, 2.0, -2, -2.0, True, False],
        'name': ['floor_divide', 'fmod', 'remainder'],
        'dtype': [numpy.float64],
        'use_dtype': [True, False],
    })
))
class TestArithmeticBinary2(ArithmeticBinaryBase, unittest.TestCase):

    def test_binary(self):
        if (self.use_dtype and
                numpy.lib.NumpyVersion(numpy.__version__) < '1.10.0'):
            raise unittest.SkipTest('Test for numpy>=1.10')
        self.check_binary()

class TestArithmeticModf(unittest.TestCase):

    @testing.for_float_dtypes()
    @testing.numpy_cupy_allclose()
    def test_modf(self, xp, dtype):
        a = xp.array([-2.5, -1.5, -0.5, 0, 0.5, 1.5, 2.5], dtype=dtype)
        b, c = xp.modf(a)
        d = xp.empty((2, 7), dtype=dtype)
        d[0] = b
        d[1] = c
        return d

@testing.parameterize(*testing.product({
    'xp': [numpy, cupy],
    'shape': [(3, 2), (), (3, 0, 2)]
}))
@testing.gpu
class TestBoolSubtract(unittest.TestCase):

    def test_bool_subtract(self):
        xp = self.xp
        if xp is numpy and not testing.numpy_satisfies('>=1.14.0'):
            raise unittest.SkipTest('NumPy<1.14.0')
        shape = self.shape
        x = testing.shaped_random(shape, xp, dtype=numpy.bool_)
        y = testing.shaped_random(shape, xp, dtype=numpy.bool_)
        with pytest.raises(TypeError):
            xp.subtract(x, y)
<|endoftext|>


<commit_before>import unittest

from cupy import testing

@testing.gpu
class TestMeanVar(unittest.TestCase):

    _multiprocess_can_split_ = True

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(rtol=1e-3)
    def test_mean_all(self, xpy, dtype):
        a = testing.shaped_arange((2, 3), xpy, dtype)
        #a /= dtype(10)
        print (xpy, a.mean())
        return a.mean()

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(rtol=1e-3)
    def test_mean_axis(self, xpy, dtype):
        a = testing.shaped_arange((2, 3, 4), xpy, dtype)
        #a /= dtype(10)
        return a.mean(axis=1)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(rtol=1e-3)
    def test_var_all(self, xpy, dtype):
        a = testing.shaped_arange((2, 3), xpy, dtype)
        #a /= dtype(10)
        return a.var()

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(rtol=1e-3)
    def test_var_axis(self, xpy, dtype):
        a = testing.shaped_arange((2, 3, 4), xpy, dtype)
        #a /= dtype(10)
        return a.var(axis=1)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(rtol=1e-3)
    def test_std_all(self, xpy, dtype):
        a = testing.shaped_arange((2, 3), xpy, dtype)
        #a /= dtype(10)
        return a.std()

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(rtol=1e-3)
    def test_std_axis(self, xpy, dtype):
        a = testing.shaped_arange((2, 3, 4), xpy, dtype)
        #a /= dtype(10)
        print a.std(axis=1), xpy, dtype
        return a.std(axis=1)
<commit_msg>Fix meanvar tests<commit_after>import unittest

from cupy import testing

@testing.gpu
class TestMeanVar(unittest.TestCase):

    _multiprocess_can_split_ = True

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose()
    def test_mean_all(self, xpy, dtype):
        a = testing.shaped_arange((2, 3), xpy, dtype)
        return a.mean()

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose()
    def test_mean_axis(self, xpy, dtype):
        a = testing.shaped_arange((2, 3, 4), xpy, dtype)
        return a.mean(axis=1)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose()
    def test_var_all(self, xpy, dtype):
        a = testing.shaped_arange((2, 3), xpy, dtype)
        return a.var()

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose()
    def test_var_axis(self, xpy, dtype):
        a = testing.shaped_arange((2, 3, 4), xpy, dtype)
        return a.var(axis=1)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose()
    def test_std_all(self, xpy, dtype):
        a = testing.shaped_arange((2, 3), xpy, dtype)
        return a.std()

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose()
    def test_std_axis(self, xpy, dtype):
        a = testing.shaped_arange((2, 3, 4), xpy, dtype)
        return a.std(axis=1)
<|endoftext|>


<commit_before>#!/usr/bin/env python
#ckwg +5
# Copyright 2011 by Kitware, Inc. All Rights Reserved. Please refer to
# KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
# Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.

def log(msg):
    import sys
    sys.stderr.write("%s\n" % msg)

def ensure_exception(action, func, *args):
    got_exception = False

    try:
        func(*args)
    except:
        got_exception = True

    if not got_exception:
        log("Error: Did not get exception when %s" % action)

def test_import():
    try:
        import vistk.pipeline.process_registry
    except:
        log("Error: Failed to import the process_registry module")

def test_create():
    from vistk.pipeline import process_registry

    process_registry.ProcessRegistry.self()
    process_registry.ProcessType()
    process_registry.ProcessTypes()
    process_registry.ProcessDescription()

def test_api_calls():
    from vistk.pipeline import config
    from vistk.pipeline import modules
    from vistk.pipeline import process_registry

    modules.load_known_modules()

    reg = process_registry.ProcessRegistry.self()

    proc_type = 'orphan'
    c = config.empty_config()

    reg.create_process(proc_type, c)
    reg.types()
    reg.description(proc_type)

def example_process():
    from vistk.pipeline import process

    class PythonExample(process.PythonProcess):
        def __init__(self, conf):
            process.PythonProcess.__init__(self, conf)

            self.ran_init = False
            self.ran_step = False
            self.ran_connect_input_port = False
            self.ran_connect_output_port = False
            self.ran_input_ports = False
            self.ran_output_ports = False
            self.ran_input_port_info = False
            self.ran_output_port_info = False
            self.ran_available_config = False
            self.ran_conf_info = False

        def _init(self):
            self.ran_init = True

            self._base_init()

        def _step(self):
            self.ran_step = True

            self.check()

            self._base_step()

        def _connect_input_port(self, port, edge):
            self.ran_connect_input_port = True

            self._base_connect_input_port(port, edge)

        def _connect_output_port(self, port, edge):
            self.ran_connect_output_port = True

            self._base_connect_output_port(port, edge)

        def _input_ports(self):
            self.ran_input_ports = True

            return self._base_input_ports()

        def _output_ports(self):
            self.ran_output_ports = True

            return self._base_output_ports()

        def _input_port_info(self, port):
            self.ran_input_port_info = True

            return self._base_input_port_info(port)

        def _output_port_info(self, port):
            self.ran_output_port_info = True

            return self._base_output_port_info(port)

        def _available_config(self):
            self.ran_available_config = True

            return self._base_available_config()

        def _config_info(self, key):
            self.ran_conf_info = True

            return self._base_conf_info(key)

        def check(self):
            if not self.ran_init:
                log("Error: _init override was not called")
            if not self.ran_step:
                log("Error: _step override was not called")
            if not self.ran_connect_input_port:
                log("Error: _connect_input_port override was not called")
            if not self.ran_connect_output_port:
                log("Error: _connect_output_port override was not called")
            if not self.ran_input_ports:
                log("Error: _input_ports override was not called")
            if not self.ran_output_ports:
                log("Error: _output_ports override was not called")
            if not self.ran_input_port_info:
                log("Error: _input_port_info override was not called")
            if not self.ran_output_port_info:
                log("Error: _output_port_info override was not called")
            if not self.ran_available_config:
                log("Error: _available_config override was not called")
            if not self.ran_conf_info:
                log("Error: _conf_info override was not called")

    return PythonExample

def base_example_process():
    from vistk.pipeline import process

    class PythonBaseExample(process.PythonProcess):
        def __init__(self, conf):
            process.PythonProcess.__init__(self, conf)

    return PythonBaseExample

def test_register():
    from vistk.pipeline import config
    from vistk.pipeline import process
    from vistk.pipeline import process_registry

    proc_type = 'python_example'
    proc_desc = 'simple description'

    reg = process_registry.ProcessRegistry.self()

    reg.register_process(proc_type, proc_desc, example_process())

    if not proc_desc == reg.description(proc_type):
        log("Error: Description was not preserved when registering")

    c = config.empty_config()

    try:
        reg.create_process(proc_type, c)
    except:
        log("Error: Could not create newly registered process type")

def test_wrapper_api():
    from vistk.pipeline import config
    from vistk.pipeline import edge
    from vistk.pipeline import process
    from vistk.pipeline import process_registry

    proc_type = 'python_example'
    proc_desc = 'simple description'

    proc_base_type = 'python_base_example'
    proc_base_desc = 'simple base description'

    iport = 'no_such_iport'
    oport = 'no_such_oport'
    key = 'no_such_key'

    reg = process_registry.ProcessRegistry.self()

    reg.register_process(proc_type, proc_desc, example_process())
    reg.register_process(proc_base_type, proc_base_desc, base_example_process())

    c = config.empty_config()

    def check_process(p):
        p.input_ports()
        p.output_ports()
        ensure_exception("asking for info on a non-existant input port",
                         p.input_port_info, iport)
        ensure_exception("asking for info on a non-existant output port",
                         p.output_port_info, oport)

        e = edge.Edge(c)

        ensure_exception("connecting to a non-existant input port",
                         p.connect_input_port, iport, e)
        ensure_exception("connecting to a non-existant output port",
                         p.connect_output_port, oport, e)

        p.available_config()
        ensure_exception("asking for info on a non-existant config key",
                         p.config_info, key)

        p.init()
        p.step()

    p = reg.create_process(proc_type, c)
    check_process(p)

    p = reg.create_process(proc_base_type, c)
    check_process(p)

def main(testname):
    if testname == 'import':
        test_import()
    elif testname == 'create':
        test_create()
    elif testname == 'api_calls':
        test_api_calls()
    elif testname == 'register':
        test_register()
    elif testname == 'wrapper_api':
        test_wrapper_api()
    else:
        log("Error: No such test '%s'" % testname)

if __name__ == '__main__':
    import os
    import sys

    if not len(sys.argv) == 4:
        log("Error: Expected three arguments")
        sys.exit(1)

    testname = sys.argv[1]

    os.chdir(sys.argv[2])

    sys.path.append(sys.argv[3])

    try:
        main(testname)
    except BaseException as e:
        log("Error: Unexpected exception: %s" % str(e))
<commit_msg>Check the process<commit_after>#!/usr/bin/env python
#ckwg +5
# Copyright 2011 by Kitware, Inc. All Rights Reserved. Please refer to
# KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
# Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.

def log(msg):
    import sys
    sys.stderr.write("%s\n" % msg)

def ensure_exception(action, func, *args):
    got_exception = False

    try:
        func(*args)
    except:
        got_exception = True

    if not got_exception:
        log("Error: Did not get exception when %s" % action)

def test_import():
    try:
        import vistk.pipeline.process_registry
    except:
        log("Error: Failed to import the process_registry module")

def test_create():
    from vistk.pipeline import process_registry

    process_registry.ProcessRegistry.self()
    process_registry.ProcessType()
    process_registry.ProcessTypes()
    process_registry.ProcessDescription()

def test_api_calls():
    from vistk.pipeline import config
    from vistk.pipeline import modules
    from vistk.pipeline import process_registry

    modules.load_known_modules()

    reg = process_registry.ProcessRegistry.self()

    proc_type = 'orphan'
    c = config.empty_config()

    reg.create_process(proc_type, c)
    reg.types()
    reg.description(proc_type)

def example_process():
    from vistk.pipeline import process

    class PythonExample(process.PythonProcess):
        def __init__(self, conf):
            process.PythonProcess.__init__(self, conf)

            self.ran_init = False
            self.ran_step = False
            self.ran_connect_input_port = False
            self.ran_connect_output_port = False
            self.ran_input_ports = False
            self.ran_output_ports = False
            self.ran_input_port_info = False
            self.ran_output_port_info = False
            self.ran_available_config = False
            self.ran_conf_info = False

        def _init(self):
            self.ran_init = True

            self._base_init()

        def _step(self):
            self.ran_step = True

            self.check()

            self._base_step()

        def _connect_input_port(self, port, edge):
            self.ran_connect_input_port = True

            self._base_connect_input_port(port, edge)

        def _connect_output_port(self, port, edge):
            self.ran_connect_output_port = True

            self._base_connect_output_port(port, edge)

        def _input_ports(self):
            self.ran_input_ports = True

            return self._base_input_ports()

        def _output_ports(self):
            self.ran_output_ports = True

            return self._base_output_ports()

        def _input_port_info(self, port):
            self.ran_input_port_info = True

            return self._base_input_port_info(port)

        def _output_port_info(self, port):
            self.ran_output_port_info = True

            return self._base_output_port_info(port)

        def _available_config(self):
            self.ran_available_config = True

            return self._base_available_config()

        def _config_info(self, key):
            self.ran_conf_info = True

            return self._base_conf_info(key)

        def check(self):
            if not self.ran_init:
                log("Error: _init override was not called")
            if not self.ran_step:
                log("Error: _step override was not called")
            if not self.ran_connect_input_port:
                log("Error: _connect_input_port override was not called")
            if not self.ran_connect_output_port:
                log("Error: _connect_output_port override was not called")
            if not self.ran_input_ports:
                log("Error: _input_ports override was not called")
            if not self.ran_output_ports:
                log("Error: _output_ports override was not called")
            if not self.ran_input_port_info:
                log("Error: _input_port_info override was not called")
            if not self.ran_output_port_info:
                log("Error: _output_port_info override was not called")
            if not self.ran_available_config:
                log("Error: _available_config override was not called")
            if not self.ran_conf_info:
                log("Error: _conf_info override was not called")

    return PythonExample

def base_example_process():
    from vistk.pipeline import process

    class PythonBaseExample(process.PythonProcess):
        def __init__(self, conf):
            process.PythonProcess.__init__(self, conf)

        def check(self):
            pass

    return PythonBaseExample

def test_register():
    from vistk.pipeline import config
    from vistk.pipeline import process
    from vistk.pipeline import process_registry

    proc_type = 'python_example'
    proc_desc = 'simple description'

    reg = process_registry.ProcessRegistry.self()

    reg.register_process(proc_type, proc_desc, example_process())

    if not proc_desc == reg.description(proc_type):
        log("Error: Description was not preserved when registering")

    c = config.empty_config()

    try:
        reg.create_process(proc_type, c)
    except:
        log("Error: Could not create newly registered process type")

def test_wrapper_api():
    from vistk.pipeline import config
    from vistk.pipeline import edge
    from vistk.pipeline import process
    from vistk.pipeline import process_registry

    proc_type = 'python_example'
    proc_desc = 'simple description'

    proc_base_type = 'python_base_example'
    proc_base_desc = 'simple base description'

    iport = 'no_such_iport'
    oport = 'no_such_oport'
    key = 'no_such_key'

    reg = process_registry.ProcessRegistry.self()

    reg.register_process(proc_type, proc_desc, example_process())
    reg.register_process(proc_base_type, proc_base_desc, base_example_process())

    c = config.empty_config()

    def check_process(p):
        p.input_ports()
        p.output_ports()
        ensure_exception("asking for info on a non-existant input port",
                         p.input_port_info, iport)
        ensure_exception("asking for info on a non-existant output port",
                         p.output_port_info, oport)

        e = edge.Edge(c)

        ensure_exception("connecting to a non-existant input port",
                         p.connect_input_port, iport, e)
        ensure_exception("connecting to a non-existant output port",
                         p.connect_output_port, oport, e)

        p.available_config()
        ensure_exception("asking for info on a non-existant config key",
                         p.config_info, key)

        p.init()
        p.step()

        p.check()

    p = reg.create_process(proc_type, c)
    check_process(p)

    p = reg.create_process(proc_base_type, c)
    check_process(p)

def main(testname):
    if testname == 'import':
        test_import()
    elif testname == 'create':
        test_create()
    elif testname == 'api_calls':
        test_api_calls()
    elif testname == 'register':
        test_register()
    elif testname == 'wrapper_api':
        test_wrapper_api()
    else:
        log("Error: No such test '%s'" % testname)

if __name__ == '__main__':
    import os
    import sys

    if not len(sys.argv) == 4:
        log("Error: Expected three arguments")
        sys.exit(1)

    testname = sys.argv[1]

    os.chdir(sys.argv[2])

    sys.path.append(sys.argv[3])

    try:
        main(testname)
    except BaseException as e:
        log("Error: Unexpected exception: %s" % str(e))
<|endoftext|>


<commit_before>#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2013, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

"""NuPIC random module tests."""

from __future__ import with_statement

import cPickle as pickle
import unittest

import numpy

from nupic.bindings.math import Random, StdRandom

class TestNupicRandom(unittest.TestCase):

  def testNupicRandomPickling(self):
    """Test pickling / unpickling of NuPIC randomness."""

    # Simple test: make sure that dumping / loading works...
    r = Random(42)
    pickledR = pickle.dumps(r)

    test1 = [r.getUInt32() for _ in xrange(10)]
    r = pickle.loads(pickledR)
    test2 = [r.getUInt32() for _ in xrange(10)]

    self.assertEqual(test1, test2,
                     "Simple NuPIC random pickle/unpickle failed.")

    # A little tricker: dump / load _after_ some numbers have been generated
    # (in the first test).  Things should still work...
    # ...the idea of this test is to make sure that the pickle code isn't just
    # saving the initial seed...
    pickledR = pickle.dumps(r)

    test3 = [r.getUInt32() for _ in xrange(10)]
    r = pickle.loads(pickledR)
    test4 = [r.getUInt32() for _ in xrange(10)]

    self.assertEqual(
        test3, test4,
        "NuPIC random pickle/unpickle didn't work for saving later state.")

    self.assertNotEqual(test1, test3,
                        "NuPIC random gave the same result twice?!?")

  def testStdRandomStateFunctions(self):
    """Test the NuPIC StdRandom to make sure getstate / setstate works."""
    sr = StdRandom(43)

    srState = sr.getstate()
    r1 = sr.random()
    r2 = sr.random()
    sr.setstate(srState)

    self.assertEqual(sr.random(), r1)
    self.assertEqual(sr.random(), r2)

    srState = sr.getstate()
    r1 = sr.random()
    r2 = sr.random()
    sr.setstate(srState)

    self.assertEqual(sr.random(), r1)
    self.assertEqual(sr.random(), r2)

  def testSample(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([2], dtype="uint32")

    r.sample(population, choices)

    self.assertEqual(choices[0], 2)
    self.assertEqual(choices[1], 4)

  def testSampleNone(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([0], dtype="uint32")

    # Just make sure there is no exception thrown.
    r.sample(population, choices)

    self.assertEqual(choices.size, 0)

  def testSampleAll(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([4], dtype="uint32")

    r.sample(population, choices)

    self.assertEqual(choices[0], 1)
    self.assertEqual(choices[1], 2)
    self.assertEqual(choices[2], 3)
    self.assertEqual(choices[3], 4)

  def testSampleWrongDimensionsPopulation(self):
    """Check that passing a multi-dimensional array throws a ValueError."""
    r = Random(42)
    population = numpy.array([[1, 2], [3, 4]], dtype="uint32")
    choices = numpy.zeros([2], dtype="uint32")

    with self.assertRaises(ValueError):
      r.sample(population, choices)

  def testSampleWrongDimensionsChoices(self):
    """Check that passing a multi-dimensional array throws a ValueError."""
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([2, 2], dtype="uint32")

    with self.assertRaises(ValueError):
      r.sample(population, choices)

  def testSampleSequenceRaisesTypeError(self):
    """Check that passing lists throws a TypeError.

    This behavior may change if sample is extended to understand sequences.
    """
    r = Random(42)
    population = [1, 2, 3, 4]
    choices = [0, 0]

    with self.assertRaises(TypeError):
      r.sample(population, choices)

  def testSampleBadDtype(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="int64")
    choices = numpy.zeros([2], dtype="int64")

    with self.assertRaises(TypeError):
      r.sample(population, choices)

  def testSampleDifferentDtypes(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([2], dtype="uint64")

    with self.assertRaises(ValueError):
      r.sample(population, choices)

  def testSamplePopulationTooSmall(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([5], dtype="uint32")

    with self.assertRaises(ValueError) as exc:
      r.sample(population, choices)

    self.assertEqual(exc.exception.message,
                     "population size must be greater than number of choices")

  def testShuffle(self):
    r = Random(42)
    arr = numpy.array([1, 2, 3, 4], dtype="uint32")

    r.shuffle(arr)

    self.assertSequenceEqual(list(arr), (3, 4, 2, 1))

  def testShuffleEmpty(self):
    r = Random(42)
    arr = numpy.zeros([0], dtype="uint32")

    r.shuffle(arr)

    self.assertEqual(arr.size, 0)

  def testShuffleEmpty(self):
    r = Random(42)
    arr = numpy.zeros([2, 2], dtype="uint32")

    with self.assertRaises(ValueError):
      r.shuffle(arr)

  def testShuffleBadDtype(self):
    r = Random(42)
    arr = numpy.array([1, 2, 3, 4], dtype="int64")

    with self.assertRaises(ValueError):
      r.shuffle(arr)

if __name__ == "__main__":
  unittest.main()
<commit_msg>Removes use of with statement in tests.<commit_after>#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2013, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

"""NuPIC random module tests."""

import cPickle as pickle
import unittest

import numpy

from nupic.bindings.math import Random, StdRandom

class TestNupicRandom(unittest.TestCase):

  def testNupicRandomPickling(self):
    """Test pickling / unpickling of NuPIC randomness."""

    # Simple test: make sure that dumping / loading works...
    r = Random(42)
    pickledR = pickle.dumps(r)

    test1 = [r.getUInt32() for _ in xrange(10)]
    r = pickle.loads(pickledR)
    test2 = [r.getUInt32() for _ in xrange(10)]

    self.assertEqual(test1, test2,
                     "Simple NuPIC random pickle/unpickle failed.")

    # A little tricker: dump / load _after_ some numbers have been generated
    # (in the first test).  Things should still work...
    # ...the idea of this test is to make sure that the pickle code isn't just
    # saving the initial seed...
    pickledR = pickle.dumps(r)

    test3 = [r.getUInt32() for _ in xrange(10)]
    r = pickle.loads(pickledR)
    test4 = [r.getUInt32() for _ in xrange(10)]

    self.assertEqual(
        test3, test4,
        "NuPIC random pickle/unpickle didn't work for saving later state.")

    self.assertNotEqual(test1, test3,
                        "NuPIC random gave the same result twice?!?")

  def testStdRandomStateFunctions(self):
    """Test the NuPIC StdRandom to make sure getstate / setstate works."""
    sr = StdRandom(43)

    srState = sr.getstate()
    r1 = sr.random()
    r2 = sr.random()
    sr.setstate(srState)

    self.assertEqual(sr.random(), r1)
    self.assertEqual(sr.random(), r2)

    srState = sr.getstate()
    r1 = sr.random()
    r2 = sr.random()
    sr.setstate(srState)

    self.assertEqual(sr.random(), r1)
    self.assertEqual(sr.random(), r2)

  def testSample(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([2], dtype="uint32")

    r.sample(population, choices)

    self.assertEqual(choices[0], 2)
    self.assertEqual(choices[1], 4)

  def testSampleNone(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([0], dtype="uint32")

    # Just make sure there is no exception thrown.
    r.sample(population, choices)

    self.assertEqual(choices.size, 0)

  def testSampleAll(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([4], dtype="uint32")

    r.sample(population, choices)

    self.assertEqual(choices[0], 1)
    self.assertEqual(choices[1], 2)
    self.assertEqual(choices[2], 3)
    self.assertEqual(choices[3], 4)

  def testSampleWrongDimensionsPopulation(self):
    """Check that passing a multi-dimensional array throws a ValueError."""
    r = Random(42)
    population = numpy.array([[1, 2], [3, 4]], dtype="uint32")
    choices = numpy.zeros([2], dtype="uint32")

    self.assertRaises(ValueError, r.sample, population, choices)

  def testSampleWrongDimensionsChoices(self):
    """Check that passing a multi-dimensional array throws a ValueError."""
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([2, 2], dtype="uint32")

    self.assertRaises(ValueError, r.sample, population, choices)

  def testSampleSequenceRaisesTypeError(self):
    """Check that passing lists throws a TypeError.

    This behavior may change if sample is extended to understand sequences.
    """
    r = Random(42)
    population = [1, 2, 3, 4]
    choices = [0, 0]

    self.assertRaises(TypeError, r.sample, population, choices)

  def testSampleBadDtype(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="int64")
    choices = numpy.zeros([2], dtype="int64")

    self.assertRaises(TypeError, r.sample, population, choices)

  def testSampleDifferentDtypes(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([2], dtype="uint64")

    self.assertRaises(ValueError, r.sample, population, choices)

  def testSamplePopulationTooSmall(self):
    r = Random(42)
    population = numpy.array([1, 2, 3, 4], dtype="uint32")
    choices = numpy.zeros([5], dtype="uint32")

    self.assertRaisesRegexp(
        ValueError, "population size must be greater than number of choices",
        r.sample, population, choices)

  def testShuffle(self):
    r = Random(42)
    arr = numpy.array([1, 2, 3, 4], dtype="uint32")

    r.shuffle(arr)

    self.assertSequenceEqual(list(arr), (3, 4, 2, 1))

  def testShuffleEmpty(self):
    r = Random(42)
    arr = numpy.zeros([0], dtype="uint32")

    r.shuffle(arr)

    self.assertEqual(arr.size, 0)

  def testShuffleEmpty(self):
    r = Random(42)
    arr = numpy.zeros([2, 2], dtype="uint32")

    self.assertRaises(ValueError, r.shuffle, arr)

  def testShuffleBadDtype(self):
    r = Random(42)
    arr = numpy.array([1, 2, 3, 4], dtype="int64")

    self.assertRaises(ValueError, r.shuffle, arr)

if __name__ == "__main__":
  unittest.main()
<|endoftext|>