"""Tests for pyxdg2.basedir."""
import contextlib
import importlib
import os
import pathlib
import shutil
import tempfile
from unittest import mock

import pytest
from hypothesis import given
from hypothesis import strategies as st
from pytest_check import check

from pyxdg2 import basedir

VALID_SUB_PATHS = st.text(
    alphabet=st.characters(blacklist_categories=["C"]), min_size=1
).filter(lambda x: not x.startswith("/"))

@contextlib.contextmanager
def fake_environ(control=None):
    control = control or {}
    with mock.patch("os.environ", new=mock.MagicMock(spec_set=dict)):
        mock_env = os.environ
        mock_env.__setitem__.side_effect = control.__setitem__
        mock_env.__getitem__.side_effect = control.__getitem__
        mock_env.get.side_effect = control.get
        yield mock_env, control

class TestGetPath:
    @given(fallback=st.builds(pathlib.Path))
    def test_falls_back_when_none(self, fallback):
        with fake_environ() as (mock_env, control):
            actual = basedir.get_path(None, fallback)

        mock_env.get.assert_not_called()
        assert fallback == actual

    @given(fallback=st.builds(pathlib.Path))
    def test_falls_back_when_invalid(self, fallback):
        with fake_environ() as (mock_env, control):
            actual = basedir.get_path("PATH", fallback)

        mock_env.get.assert_called_once_with("PATH")
        assert fallback == actual

    def test_no_fallback_when_invalid(self):
        with fake_environ(), pytest.raises(KeyError):
            basedir.get_path("anything, really", None)

    @given(
        control=st.dictionaries(st.text(min_size=1), st.text(min_size=1), min_size=1),
        rng=st.randoms(),
    )
    def test_gets_from_environment(self, control, rng):
        with fake_environ(control) as (mock_env, _):
            variable = rng.choice(list(control.keys()))

            actual = basedir.get_path(variable, None)

        assert pathlib.Path(control[variable]) == actual

class TestGenPaths:
    path_params = [
        pytest.param(
            [pathlib.Path("/"), pathlib.Path("/usr"), pathlib.Path("/bin")], id="basic"
        )
    ]

    def test_falls_back(self):
        with fake_environ() as (mock_env, control):
            actual = list(basedir.gen_paths("", "/"))

        assert [pathlib.Path("/")] == actual

    def test_totally_invalid(self):
        with fake_environ(), pytest.raises(KeyError):
            list(basedir.gen_paths("", None))

    @pytest.mark.parametrize("paths", path_params)
    def test_fallback_outputs_same_as_inputs(self, paths):
        path_spec = ":".join(path.as_posix() for path in paths)

        with fake_environ() as (mock_env, _):
            actual = list(basedir.gen_paths("", path_spec))

        assert paths == actual

    @pytest.mark.parametrize("paths", path_params)
    @pytest.mark.parametrize("env_var", ["SOME_VAR"])
    def test_env_outputs_same_as_inputs(self, paths, env_var):
        path_spec = ":".join(path.as_posix() for path in paths)
        with fake_environ() as (mock_env, control):
            control[env_var] = path_spec

            actual = list(basedir.gen_paths(env_var))

        assert paths == actual

class TestEnvVarPaths:
    environment_specs = [
        pytest.param(
            {
                "HOME": "/",
                "XDG_DATA_HOME": "/",
                "XDG_CONFIG_HOME": "/",
                "XDG_STATE_HOME": "/",
                "XDG_CACHE_HOME": "/",
                "XDG_DATA_DIRS": "/",
                "XDG_CONFIG_DIRS": "/",
                "XDG_RUNTIME_DIR": "/",
            },
            id="everything_/",
        ),
    ]

    @pytest.mark.parametrize("control", environment_specs)
    def test_get_vars(self, control):
        patch_home = mock.patch(
            "pathlib.Path.home", return_value=pathlib.Path(control["HOME"])
        )
        with fake_environ(control) as (mock_env, _), patch_home:
            importlib.reload(basedir)

        check.equal(basedir.HOME.as_posix(), control["HOME"])
        check.equal(basedir.XDG_DATA_HOME.as_posix(), control["XDG_DATA_HOME"])
        check.equal(basedir.XDG_CONFIG_HOME.as_posix(), control["XDG_CONFIG_HOME"])
        check.equal(basedir.XDG_STATE_HOME.as_posix(), control["XDG_STATE_HOME"])
        check.equal(basedir.XDG_CACHE_HOME.as_posix(), control["XDG_CACHE_HOME"])
        check.equal(basedir.XDG_DATA_DIRS[0].as_posix(), control["XDG_DATA_DIRS"])
        check.equal(basedir.XDG_CONFIG_DIRS[0].as_posix(), control["XDG_CONFIG_DIRS"])
        check.equal(basedir.XDG_RUNTIME_DIR.as_posix(), control["XDG_RUNTIME_DIR"])

    @pytest.mark.parametrize("home_path", ["/"])
    def test_vars_unset(self, home_path):
        patch_getuid = mock.patch("os.getuid", return_value=1000)
        patch_home = mock.patch(
            "pathlib.Path.home", return_value=pathlib.Path(home_path)
        )
        with fake_environ({"HOME": home_path}), patch_getuid, patch_home:
            importlib.reload(basedir)

        check.equal(basedir.HOME.as_posix(), home_path)
        check.equal(basedir.XDG_DATA_HOME, pathlib.Path(home_path, ".local/share"))
        check.equal(basedir.XDG_CONFIG_HOME, pathlib.Path(home_path, ".config"))
        check.equal(basedir.XDG_STATE_HOME, pathlib.Path(home_path, ".local/state"))
        check.equal(basedir.XDG_CACHE_HOME, pathlib.Path(home_path, ".cache"))
        check.equal(
            basedir.XDG_DATA_DIRS,
            [pathlib.Path("/usr/local/share"), pathlib.Path("/usr/share")],
        )
        check.equal(basedir.XDG_CONFIG_DIRS, [pathlib.Path("/etc/xdg")])
        check.equal(
            basedir.XDG_RUNTIME_DIR, pathlib.Path("/tmp/user-1000")  # noqa=S108
        )

class TestEnsureResource:
    @given(
        st.lists(VALID_SUB_PATHS.filter(lambda x: x != "."), min_size=1)  # noqa=PLR2004
    )
    def test_creates_paths(self, sub_paths):
        with tempfile.TemporaryDirectory() as tempdir:
            temp_path = pathlib.Path(tempdir)
            expected = temp_path.joinpath(*sub_paths)
            assert not expected.exists(), "Oops this path isn't supposed to exist"

            actual = basedir.ensure_resource(temp_path, *sub_paths)

            check.equal(expected, actual)
            check.is_true(actual.exists())

    @given(st.lists(VALID_SUB_PATHS, min_size=1))
    def test_returns_existing_path(self, sub_paths):
        with tempfile.TemporaryDirectory() as tempdir:
            temp_path = pathlib.Path(tempdir)
            expected = temp_path.joinpath(*sub_paths)
            expected.mkdir(parents=True, exist_ok=True)

            actual = basedir.ensure_resource(temp_path, *sub_paths)

            check.equal(expected, actual)
            check.is_true(actual.exists())

    def test_fails_when_mkdir_fails(self):
        with tempfile.TemporaryDirectory() as tempdir:
            temp_path = pathlib.Path(tempdir)
            expected = temp_path.joinpath("cant_touch_dis")
            expected.mkdir(mode=0)

            with pytest.raises(PermissionError):
                basedir.ensure_resource(temp_path, "cant_touch_dis/no")

    def test_fails_when_not_relative(self):
        with pytest.raises(ValueError, match=r"'/'.*'/home'"):
            basedir.ensure_resource(pathlib.Path("/home"), "/")

class TestFindResource:
    def test_empty_base_paths(self):
        assert not list(basedir.find_resource([]))

    @given(st.lists(VALID_SUB_PATHS, min_size=1))
    def test_sub_paths_nonexistent(self, sub_paths):
        with tempfile.TemporaryDirectory() as tempdir:
            temp_path = pathlib.Path(tempdir)

            assert not list(basedir.find_resource([temp_path], *sub_paths))

    @given(st.lists(VALID_SUB_PATHS, min_size=1))
    def test_subpath_exists(self, sub_paths):
        with tempfile.TemporaryDirectory() as tempdir:
            temp_path = pathlib.Path(tempdir)
            expected = temp_path.joinpath(*sub_paths)
            expected.mkdir(parents=True, exist_ok=True)

            actual = list(basedir.find_resource([temp_path], *sub_paths))

            assert [expected] == actual

    @given(st.lists(VALID_SUB_PATHS, min_size=1), st.randoms())
    def test_some_subpaths(self, sub_paths, rng):
        temp_paths = []
        try:
            for _i in range(rng.randint(2, 10)):
                temp_paths.append(pathlib.Path(tempfile.mkdtemp()))
            expected_parents = rng.choices(temp_paths, rng.randint(0, len(temp_paths)))
            expected_paths = [path.joinpath(*sub_paths) for path in expected_parents]
            for path in expected_paths:
                path.mkdir(parents=True)

            actual = list(basedir.find_resource(temp_paths, *sub_paths))

            assert expected_paths == actual

        finally:
            for path in temp_paths:
                shutil.rmtree(path, ignore_errors=True)


from django.urls import path, include
from main.views import CityViewSet, CountryViewSet, ImageViewSet, OptionViewSet, AutoMarkViewSet, AutoModelViewSet, AutoSaloonViewSet, ComplectationViewSet, OfferViewSet, DiscountViewSet, test
from rest_framework import routers
from rest_framework_jwt.views import obtain_jwt_token

urlpatterns = [
    path('login/', obtain_jwt_token),
    path('test/', test),
]

router = routers.DefaultRouter()
router.register('cities', CityViewSet, )
router.register('countries', CountryViewSet, )
router.register('images', ImageViewSet, )
router.register('options', OptionViewSet, )
router.register('marks', AutoMarkViewSet, )
router.register('models', AutoModelViewSet, )
router.register('saloons', AutoSaloonViewSet, )
router.register('complectations', ComplectationViewSet, )
router.register('offers', OfferViewSet, )
router.register('discounts', DiscountViewSet, )

urlpatterns += router.urls


import BigWorld, FMOD
from collections import namedtuple
import struct
from constants import ARENA_GUI_TYPE
from tutorial.control import context
from tutorial.control.context import ClientCtx, GlobalStorage
from tutorial.logger import LOG_DEBUG, LOG_ERROR, LOG_WARNING
BATTLE_RECORDS = ['completed',
 'failed',
 'accCompleted',
 'startedAt',
 'chapterIdx']
EXTENDED_BATTLE_RECORDS = ['playerTeam',
 'winnerTeam',
 'finishReason',
 'vTypeCD',
 'arenaTypeID',
 'arenaUniqueID']
ALL_BATTLE_RECORDS = BATTLE_RECORDS + EXTENDED_BATTLE_RECORDS
BATTLE_RECORDS_FORMAT = '3ifh'
ALL_BATTLE_RECORDS_FORMAT = '3if4h2iQ'

class TRAINING_RESULT_KEY:
    FINISHED = 'finished'
    FAILED = 'failed'

class TRAINING_FINISH_REASON_KEY:
    FINISHED = 'finished'
    FAILED = 'failed'
    TIMEOUT = 'timeout'
    EXTERMINATION = 'extermination'

class BattleClientCtx(ClientCtx, namedtuple('BattleClientCtx', BATTLE_RECORDS)):

    @classmethod
    def _makeDefault(cls):
        return cls.__new__(cls, 0, -1, -1, 0.0, -1)

    @classmethod
    def makeCtx(cls, record):
        result = cls._makeDefault()
        if record is not None and len(record):
            try:
                result = cls._make(struct.unpack(BATTLE_RECORDS_FORMAT, record))
            except struct.error:
                LOG_ERROR('Client ctx is not valid', record)

        return result

    @classmethod
    def fetch(cls, *args):
        player = BigWorld.player()
        if player is None or not hasattr(player, 'clientCtx'):
            LOG_DEBUG('Avatar.clientCtx not found', player)
            result = cls._makeDefault()
        else:
            result = cls.makeCtx(player.clientCtx)
        return result

    def _store(self):
        record = self.makeRecord()
        player = BigWorld.player()
        if player is None or not hasattr(player, 'storeClientCtx'):
            LOG_DEBUG('Avatar.storeClientCtx not found', player)
            return False
        else:
            player.storeClientCtx(record)
            return True

    def makeRecord(self):
        return struct.pack(BATTLE_RECORDS_FORMAT, *self)

    def addMask(self, mask, done = True):
        completed = self.completed
        failed = self.failed
        if done:
            completed |= mask
        else:
            failed |= mask
        newCtx = self._replace(completed=completed, failed=failed)
        if newCtx._store():
            return newCtx
        return self

    def setChapterIdx(self, chapterIdx):
        newCtx = self._replace(chapterIdx=chapterIdx)
        if newCtx._store():
            return newCtx
        return self

    def setAccCompleted(self, accCompleted):
        newCtx = self._replace(accCompleted=accCompleted)
        if newCtx._store():
            return newCtx
        return self

    def setStartedAt(self, time):
        newCtx = self._replace(startedAt=time)
        if newCtx._store():
            return newCtx
        return self

class ExtendedBattleClientCtx(ClientCtx, namedtuple('ExtendedBattleClientCtx', ALL_BATTLE_RECORDS)):

    @classmethod
    def _makeDefault(cls):
        return cls.__new__(cls, 0, -1, -1, 0.0, -1, 1, 2, -1, -1, -1, -1)

    @classmethod
    def makeCtx(cls, record):
        result = cls._makeDefault()
        if record is not None and len(record):
            try:
                result = cls._make(struct.unpack(ALL_BATTLE_RECORDS_FORMAT, record))
            except struct.error:
                LOG_ERROR('Client ctx is not valid', record)

        return result

    @classmethod
    def fetch(cls, *args):
        params = ExtendedBattleClientCtx._makeDefault()._asdict()
        player = BigWorld.player()
        params.update(BattleClientCtx.fetch()._asdict())
        arena = getattr(player, 'arena', None)
        if arena is not None:
            info = arena.periodAdditionalInfo
            if info is not None and len(info) > 1:
                params['winnerTeam'] = info[0]
                params['finishReason'] = info[1]
            params['arenaUniqueID'] = arena.arenaUniqueID
            arenaType = arena.arenaType
            if arenaType is not None:
                params['arenaTypeID'] = arenaType.id
            pVehID = getattr(player, 'playerVehicleID', None)
            vehicles = arena.vehicles
            if pVehID in vehicles:
                vDescriptor = vehicles[pVehID]['vehicleType']
                if vDescriptor is not None:
                    params['vTypeCD'] = vDescriptor.type.compactDescr
        params['playerTeam'] = getattr(player, 'team', 1)
        LOG_DEBUG('All records in context', params)
        return ExtendedBattleClientCtx(**params)

    def makeRecord(self):
        return struct.pack(ALL_BATTLE_RECORDS_FORMAT, *self)

class BattleStartReqs(context.StartReqs):

    def isEnabled(self):
        arena = getattr(BigWorld.player(), 'arena', None)
        enabled = False
        if arena is not None:
            enabled = arena.guiType == ARENA_GUI_TYPE.TUTORIAL
        return enabled

    def process(self):
        loader, ctx = self._flush()
        clientCtx = BattleClientCtx.fetch()
        ctx.bonusCompleted = clientCtx.completed
        GlobalStorage.clearVars()
        loader._doRun(ctx)

class BattleBonusesRequester(context.BonusesRequester):

    def request(self, chapterID = None):
        chapter = self.getChapter(chapterID=chapterID)
        if chapter is None:
            LOG_ERROR('Chapter not found', chapterID)
            return
        elif not chapter.hasBonus():
            LOG_ERROR('Chapter has not bonus', chapter.getID())
            return
        else:
            bonusID = chapter.getBonusID()
            mask = 1 << bonusID
            localCtx = BattleClientCtx.fetch().addMask(mask)
            if chapter.isBonusReceived(self._completed):
                LOG_DEBUG('Bonus already received', chapter.getID(), self._completed)
                self._isReceived = True
                return
            LOG_DEBUG('Received bonus', bonusID)
            self._completed |= mask
            self._gui.setTrainingProgress(self._tutorial._descriptor.getProgress(localCtx.completed))
            return

class BattleSoundPlayer(context.SoundPlayer):
    __guiSounds = {context.SOUND_EVENT.TASK_FAILED: '/GUI/notifications_FX/task_new',
     context.SOUND_EVENT.TASK_COMPLETED: '/GUI/notifications_FX/task_complete',
     context.SOUND_EVENT.NEXT_CHAPTER: '/GUI/notifications_FX/task_part_complete'}

    def __init__(self):
        super(BattleSoundPlayer, self).__init__()
        self.__ignoreNext = False
        self.__speakSnd = None
        self.__nextSpeakID = None
        self.__prevSpeaks = set()
        return

    def play(self, event, sndID = None):
        if self.isMuted():
            return
        if event in self.__guiSounds.keys():
            self._playGUI(event)
        elif event is context.SOUND_EVENT.SPEAKING:
            self._speak(sndID)
        else:
            LOG_WARNING('Sound event is not supported', event)

    def stop(self):
        self._clear()
        self.__ignoreNext = False
        self.__nextSpeakID = None
        self.__prevSpeaks.clear()
        return

    def isPlaying(self, event, sndID = None):
        result = False
        if event is context.SOUND_EVENT.SPEAKING:
            if self.__speakSnd is not None:
                if sndID is not None:
                    result = len(self.__prevSpeaks) and list(self.__prevSpeaks)[-1] == sndID
                else:
                    result = True
        else:
            LOG_WARNING('Sound event is not supported', event)
        return result

    def goToNextChapter(self):
        self.__prevSpeaks.clear()

    def _playGUI(self, event):
        if self.__ignoreNext:
            self.__ignoreNext = False
            return
        if event is context.SOUND_EVENT.NEXT_CHAPTER:
            self.__ignoreNext = True
        sndID = self.__guiSounds[event]
        sound = FMOD.getSound(sndID)
        if sound:
            sound.play()
        else:
            LOG_ERROR('Sound not found', sndID)

    def _clear(self):
        if self.__speakSnd is not None:
            self.__speakSnd.setCallback('EVENTFINISHED', None)
            self.__speakSnd.stop()
            self.__speakSnd = None
        return

    def _speak(self, sndID):
        if sndID in self.__prevSpeaks:
            LOG_DEBUG('Speaking played, ignore', sndID)
            return
        elif sndID is None:
            LOG_WARNING('Sound ID for speaking is not defined')
            return
        elif self.__speakSnd is not None:
            self.__nextSndID = sndID
            return
        else:
            sound = FMOD.getSound(sndID)
            if not sound:
                LOG_ERROR('Sound not found', sndID)
                return
            self.__nextSndID = None
            self.__speakSnd = sound
            self.__prevSpeaks.add(sndID)
            sound.setCallback('EVENTFINISHED', self.__onSpeakingStop)
            sound.play()
            return

    def __onSpeakingStop(self, sound):
        LOG_DEBUG('Stop playing sound by event', sound)
        self._clear()
        if self.__nextSndID is not None:
            self._speak(self.__nextSndID)
        return


# Standard OpenVX Data Definition dictionaries.
# There are two dictionaries:
# 1) standardAlias: Map OpenVX type enumeration names to OpenVX type names
# 2) standardTypes: Describe the OpenVX data types in detail

# Probably another day's work in this
# TODO:
# Finish tables
# TODO:
# Constants for each string to reduce size and increase efficiency

# Standard type strings and other things
s_vx_context = 'vx_context'
s_vx_graph = 'vx_graph'
s_vx_node = 'vx_node'
s_vx_kernel = 'vx_kernel'
s_vx_parameter = 'vx_parameter'
s_vx_delay = 'vx_delay'
s_vx_image = 'vx_image'
s_vx_scalar = 'vx_scalar'
s_vx_tensor = 'vx_tensor'
s_vx_reference = 'vx_reference'
s_vx_threshold = 'vx_threshold'
s_vx_lut = 'vx_lut'
s_vx_pyramid = 'vx_pyramid'
s_vx_matrix = 'vx_matrix'
s_vx_array = 'vx_array'
s_vx_convolution = 'vx_convolution'
s_vx_distribution = 'vx_distribution'
s_vx_remap = 'vx_remap'
s_vx_object_array = 'vx_object_array'
s_vx_int8 = 'vx_int8'
s_vx_uint8 = 'vx_uint8'
s_vx_int16 = 'vx_int16'
s_vx_uint16 = 'vx_uint16'
s_vx_int32 = 'vx_int32'
s_vx_uint32 = 'vx_uint32'
s_vx_int64 = 'vx_int64'
s_vx_uint64 = 'vx_uint64'
s_vx_float16 = 'vx_float16'
s_vx_float32 = 'vx_float32'
s_vx_float64 = 'vx_float64'
s_vx_char = 'vx_char'
s_vx_bool = 'vx_bool'
s_vx_size = 'vx_size'
s_vx_df_image = 'vx_df_image'
s_vx_enum = 'vx_enum'

s_vx_df_image_e = 'vx_df_image_e'
s_vx_type_e = 'vx_type_e'

s_input = 'input'
s_output = 'output'
s_input1 = 'input1'
s_input2 = 'input2'
s_in1 = 'in1'
s_in2 = 'in2'
s_out = 'out'
s_src = 'src'
s_dst = 'dst'
s_accum = 'accum'
s_policy = 'policy'
s_OBJECT = 'OBJECT'             # for defining vx_reference object types
s_BASE = 'BASE'                 # for defining base types (do we need this?)
s_OPAQUE = 'OPAQUE'             # for defining types we know nothing about
s_INHERENT = 'INHERENT'         # For defining inherent types (that we should know about)
s_ENUM = 'ENUM'                 # type of constant composed of vendor<<20, id<<12 and a constant
s_ATTRIBUTE = 'ATTRIBUTE'       # type of constant composed of vendor<<20, object type <<8 and a constant
s_KERNEL = 'KERNEL'             # type of constant composed of vendor <<20, lib<<12 and a constant
s_CONSTANT = 'CONSTANTS'        # type of constant that is just a constant
s_STRUCT = 'STRUCT'             # for defining structure types
s_UNION = 'UNION'               # for defining union types
s_ARRAY = 'ARRAY'               # for defining array types

s_VX_ID_KHRONOS = 'VX_ID_KHRONOS'

standardAlias = {
    'VX_TYPE_INVALID': 'vx_invalid',
    'VX_TYPE_CHAR': s_vx_char,
    'VX_TYPE_INT8': s_vx_int8,
    'VX_TYPE_UINT8': s_vx_uint8,
    'VX_TYPE_INT16': s_vx_int16,
    'VX_TYPE_UINT16': s_vx_uint16,
    'VX_TYPE_INT32': s_vx_int32,
    'VX_TYPE_UINT32': s_vx_uint32,
    'VX_TYPE_INT64': s_vx_int64,
    'VX_TYPE_UINT64': s_vx_uint64,
    'VX_TYPE_FLOAT16': s_vx_float16,
    'VX_TYPE_FLOAT32': s_vx_float32,
    'VX_TYPE_FLOAT64': s_vx_float64,
    'VX_TYPE_ENUM': s_vx_enum,
    'VX_TYPE_SIZE': s_vx_size,
    'VX_TYPE_DF_IMAGE': s_vx_df_image,
    'VX_TYPE_BOOL': s_vx_bool,

    'VX_TYPE_RECTANGLE': 'vx_rectangle_t',
    'VX_TYPE_KEYPOINT': 'vx_keypoint_t',
    'VX_TYPE_COORDINATES2D': 'vx_coordinates2d_t',
    'VX_TYPE_COORDINATES3D': 'vx_coordinates3d_t',
    'VX_TYPE_COORDINATES2DF': 'vx_coordinates2df_t',

    'VX_TYPE_NN_CONVOLUTION_PARAMS': 'vx_nn_convolution_params_t',
    'VX_TYPE_NN_DECONVOLUTION_PARAMS': 'vx_nn_deconvolution_params_t',
    'VX_TYPE_NN_ROI_POOL_PARAMS': 'vx_nn_roi_pool_params_t',

    'VX_TYPE_HOG_PARAMS': 'vx_hog_t',
    'VX_TYPE_HOUGH_LINES_PARAMS': 'vx_hough_lines_p_t',
    'VX_TYPE_LINE_2D': 'vx_line2d_t',
    'VX_TYPE_TENSOR_MATRIX_MULTIPLY_PARAMS': 'vx_tensor_matrix_multiply_params_t',

    'VX_TYPE_CLASSIFIER_MODEL': 'vx_classifier_model',

    'VX_TYPE_REFERENCE': s_vx_reference,
    'VX_TYPE_CONTEXT': s_vx_context,
    'VX_TYPE_GRAPH': s_vx_graph,
    'VX_TYPE_NODE': s_vx_node,
    'VX_TYPE_KERNEL': s_vx_kernel,
    'VX_TYPE_PARAMETER': s_vx_parameter,

    'VX_TYPE_DELAY': s_vx_delay,
    'VX_TYPE_LUT': s_vx_lut,
    'VX_TYPE_DISTRIBUTION': s_vx_distribution,
    'VX_TYPE_PYRAMID': s_vx_pyramid,
    'VX_TYPE_THRESHOLD': s_vx_threshold,
    'VX_TYPE_MATRIX': s_vx_matrix,
    'VX_TYPE_CONVOLUTION': s_vx_convolution,
    'VX_TYPE_SCALAR': s_vx_scalar,
    'VX_TYPE_ARRAY': s_vx_array,
    'VX_TYPE_IMAGE': s_vx_image,
    'VX_TYPE_REMAP': s_vx_remap,
    'VX_TYPE_OBJECT_ARRAY': s_vx_object_array,
    'VX_TYPE_TENSOR': s_vx_tensor
}

reverseAlias = {v:k for k,v in list(standardAlias.items())}

stClass = 0
stAttrs = 1
stType = 2
stNum = 3

class TypeDef(object):
    # Dictionary that keeps track of all typedefs
    typeDict = dict()
    userStructNext = 0x100

    def __init__(self, name, objtype, vendor=None, id=None, defs=dict(), attrs=dict(), size=None, eltype=None):
        """
        Initialise the type object and put an entry in typedict
        """
        self.objtype = objtype
        self.vendor = vendor
        self.id = id
        self.size = size
        self.eltype = eltype
        self.defs = dict()
        # Get the enum value modifier according to type
        mod = 0
        if objtype == s_KERNEL:
            # Get Vendor id
            v = TypeDef.getTypeVal('vx_vendor_id_e', vendor)
            # Get library id
            l = TypeDef.getTypeVal('vx_library_e', id)
            if v is not None and l is not None:
                mod = (v << 20) | (l << 12)
        elif objtype == s_ATTRIBUTE:
            # Get Vendor id
            v = TypeDef.getTypeVal('vx_vendor_id_e', vendor)
            # Get type id
            t = TypeDef.getTypeVal('vx_type_e', id)
            if v is not None and t is not None:
                mod = (v << 20) | (t << 8)
        elif objtype == s_ENUM:
            # Get Vendor id
            v = TypeDef.getTypeVal('vx_vendor_id_e', vendor)
            # Get enum id
            e = TypeDef.getTypeVal('vx_enum_e', id)
            if v is not None and e is not None:
                mod = (v << 20) | (e << 12)
        if isinstance(defs, list):
            # shortcut for definition when all values are consecutive
            i = 0
            for d in defs:
                self.defs[d] = i | mod
                i += 1
        elif mod != 0 and isinstance(defs, dict):
            for k, v in list(defs.items()):
                self.defs[k] = v | mod
        else:
            self.defs = defs
        self.attrs = attrs
        TypeDef.typeDict[name] = self

    @staticmethod
    def add(name, objtype, vendor=None, id=None, defs=dict(), attr=dict()):
        """
        If the entry with the given name does not exist, add it.
        Return the entry with the given name.
        """
        return TypeDef.get(name) if name in TypeDef.typedict else TypeDef(name, objtype, vendor, id, defs, attr)

    @staticmethod
    def getTypeVal(typeName, name, default=None):
        """
        Get an enum value or attribute for a given type
        """
        return TypeDef.typeDict[typeName].defs.get(name, default) if typeName in TypeDef.typeDict else default

    @staticmethod
    def getValueNameFromType(typeName, value, default=None):
        """
        Get the string for a given value in the given enumerated type
        """
        return TypeDef.typeDict[typeName].getLabel(value) if typeName in TypeDef.typeDict else default

    @staticmethod
    def getObjectTypeNameFromValue(value, default=None):
        """
        Get the string for a given type embedded in a value
        Note that this could give misleading results if used inappropriately
        """
        return TypeDef.getValueNameFromType("vx_type_e", TypeDef.getObjectTypeIdVal(value))

    @staticmethod
    def getEnumIdNameFromValue(value, default=None):
        """
        Get the name of the embedded enumeration id as given by vx_enum_e
        Note that this could give misleading results if used inappropriately
        """
        return TypeDef.getValueNameFromType("vx_enum_e", TypeDef.getEnumIdVal(value))

    @staticmethod
    def getEnumTypeNameFromValue(value, default=None):
        """
        Get the name of the enumeration type as given by the embedded enumeration id by
        looking in the definition of vx_enum (rather than vx_enum_e)
        Note that this could give misleading results if used inappropriately
        """
        return TypeDef.getValueNameFromType("vx_enum", TypeDef.getEnumIdVal(value))

    @staticmethod
    def getAttributeTypeNameFromValue(value, default=None):
        """
        Get the actual enumerated type name for an attribute value.
        """
        a = TypeDef.getObjectTypeNameFromValue(value)
        # Form an attribute enumeration name:
        # Type name is VX_TYPE_XXXX, attribute enumeration name is vx_xxxx_attribute_e
        return default if a is None else "vx_%s_attribute_e"%a[8:].lower()

    @staticmethod
    def getConstantNameFromValue(value):
        """
        Get the type and string for a given value, assuming that it is in a constant enumeration
        """
        for t, tobj in list(TypeDef.typeDict.items()):
            if tobj.objtype == s_CONSTANT:
                n = tobj.getLabel(value)
                if n is not None:
                    return t, n
        return None, None

    @staticmethod
    def getAttributeNameFromValue(value):
        """
        Get the type and string for a given value, assuming that it is in an attribute enumeration
        """
        for t, tobj in list(TypeDef.typeDict.items()):
            if tobj.objtype == s_ATTRIBUTE:
                n = tobj.getLabel(value)
                if n is not None:
                    return t, n
        return None, None

    @staticmethod
    def getEnumNameFromValue(value):
        """
        Get the type and string for a given value, assuming that it is in a kernel or enum enumeration
        """
        for t, tobj in list(TypeDef.typeDict.items()):
            if tobj.objtype in {s_ENUM, s_KERNEL}:
                n = tobj.getLabel(value)
                if n is not None:
                    return t, n
        return None, None

    @staticmethod
    def getValueNameFromValue(value):
        """
        Get the type and string for a given value without knowing the type
        Searches in the following order:
        Constants, enums & kernels, attributes, plain number as vx_uint32.
        """
        t, v = TypeDef.getConstantNameFromValue(value)
        if v is None:
            t, v = TypeDef.getEnumNameFromValue(value)
            if v is None:
                t, v = TypeDef.getAttributeNameFromValue(value)
                if v is None:
                    t, v = s_vx_uint32, value
        return t, v

    @staticmethod
    def getVendorIdVal(value):
        """
        Get the vendor id value from the given enum value
        """
        return (int(value) & 0xFFF00000) >> 20

    @staticmethod
    def getObjectTypeIdVal(value):
        """
        Get the type id from the given enum value
        """
        return (int(value) & 0xFFF00) >> 8

    @staticmethod
    def getEnumIdVal(value):
        """
        Get the enum type id from the given enum value
        """
        return (int(value) & 0xFF000) >> 12

    @staticmethod
    def getLibIdVal(value):
        """
        Get the library id from the given enum value
        """
        return (int(value) & 0xFF000) >> 12

    @staticmethod
    def get(name):
        """
        Get type information for the given name
        """
        return TypeDef.typeDict.get(name)

    @staticmethod
    def keys(name):
        """
        Return a list of the keys for the given name
        """
        return list(TypeDef.get(name).defs.keys()) if name in TypeDef.typeDict else []

    @staticmethod
    def values(name):
        """
        Return a list of the values for the given name
        """
        return list(TypeDef.get(name).defs.values()) if name in TypeDef.typeDict else []

    @staticmethod
    def items(name):
        """
        Return a list of the items for the given name
        """
        return list(TypeDef.get(name).defs.items()) if name in TypeDef.typeDict else []

    @staticmethod
    def labelsValues(name, start=None, end=None, excl=[], sortOnValues=True):
        """
        Return a list of labels and a list of values, sorted on the values
        (default) or labels if sortOnValues is False,
        and in the range start value to end value-1 inclusive but not in
        the iterable excl, which may hold either labels or values
        """
        if name in TypeDef.typeDict:
            defs = TypeDef.typeDict[name].defs
            labels = list(defs.keys())
            labels.sort(key=lambda x:defs[x] if sortOnValues else None)
            values = []
            newLabels = []
            for k in labels:
                v = defs[k]
                if (v >= start or start is None) and \
                   (v < end or end is None) and \
                   (k not in excl or v not in excl):
                    values.append(v)
                    newLabels.append(k)
            return newLabels, values
        else:
            return [], []

    @staticmethod
    def formatToString(value):
        """
        Takes a format id and returns the associated string as used in the XML
        """
        x = ""
        for i in [24, 16, 8, 0]:
            x = x + chr((value >> i) & 0xFF)
        return x

    @staticmethod
    def formatToId(value):
        """
        Takes a format string as used in teh XML and returns the associated integer id
        """
        if value is None:
            return None
        x = 0
        for c in value:
            x = (x << 8) | ord(c)
        return x

    @staticmethod
    def typeFromEnum(enumval, default=None):
        """
        Return a type name like vx_float32 from an enum name like VX_TYPE_FLOAT32
        """
        return standardAlias.get(enumval, default)

    @staticmethod
    def enumFromType(typeval, default=None):
        """
        return an enum name like VX_TYPE_FLOAT32 from a type name like vx_float32
        """
        return reverseAlias.get(typeval, default)

    @staticmethod
    def tagFromEnum(enumval):
        """
        return an XML tag name like 'float32' from an enum name like VX_TYPE_FLOAT32
        (this can have no reverse as all user structs have the same tag)
        """
        if enumval.startswith('VX_TYPE_'):
            tag = enumval[8:].lower()
        else:
            # must be a user type
            tag = 'user'
        return tag

    # @staticmethod
    # def tagFromType(typeval):
    #     """
    #     return an XML tag name like 'float32' from an enum name like VX_TYPE_FLOAT32
    #     (this can have no reverse as all user structs have the same tag)
    #     """
    #     return TypeDef.tagFromEnum(TypeDef.enumFromType(typeval))

    @staticmethod
    def addUserStruct(id, size):
        """
        Add a user struct to the vx_type_e enumeration,
        to the standard & reverse alias and as a new type,
        all if it does not yet exist.
        """
        if id not in TypeDef.typeDict:
            TypeDef.get('vx_type_e').defs[id] = TypeDef.userStructNext
            TypeDef.userStructNext += 1
            standardAlias[id] = id  # makes looking up this type a no-op
            reverseAlias[id] = id
            TypeDef(id, s_ARRAY, size=size, eltype=s_vx_uint8)

    @staticmethod
    def defaultData(name):
        """
        Get a default value for a given data type
        """
        return '0' if name == 'vx_enum' else TypeDef.get(name).getDefaultData()

    def getDefaultData(self):
        """
        Return default data for this type
        This is:
        For a struct, union or object, a dictionary
        For an array, a list of values as strings
        For an opaque type, an empty string
        Otherwise, a value as a string.
        """
        if self.objtype in {s_STRUCT, s_UNION, s_OBJECT}:
            data = {}
            for k, v in list(self.defs.items()):
                data[k] = TypeDef.defaultData(v)
            return data
        elif self.objtype == s_INHERENT:
            return self.eltype
        elif self.objtype == s_ARRAY:
            return [TypeDef.defaultData(self.eltype)] * int(self.size)
        elif self.objtype == s_OPAQUE:
            # we know nothing about this type, can only return an empty string
            return ""
        else:
            # pick the first value from the enumeration
            return str(list(self.defs.values())[0])

    def getVal(self, name, default=None):
        """
        Get an attribute value for this object.
        """
        if name in self.defs:
            return self.defs[name]
        else:
            return self.attr.get(name)

    def getLabel(self, value, default=None):
        """
        Return the label for the given enumerated value
        """
        # just look for the enum value in self.defs
        for k, v in list(self.defs.items()):
            if v == value:
                return k
        return default

    def isInherent(self):
        """
        Tests if the type is inherent
        """
        return self.objtype == s_INHERENT

    def isBase(self):
        """
        Tests if the type is a base type.
        If the type is a base type, it's defs have keys
        which are type names and values which are enum labels
        of vx_type_e, or None, if the type is not available there.
        """
        return self.objtype == s_BASE

    def isEnum(self):
        return self.objtype == s_ENUM

    def isAttribute(self):
        return self.objtype == s_ATTRIBUTE

    def isKernel(self):
        return self.objtype == s_KERNEL

    def isConstant(self):
        return self.objtype == s_CONSTANT

    def isAllEnum(self):
        """
        Tests if the type is an enumerated type, including
        attributes, kernels, and constants
        """
        return self.objtype in {s_ATTRIBUTE, s_CONSTANT, s_ENUM, s_KERNEL}

    def isStruct(self):
        """
        Tests if the type is a struct or union
        """
        return self.objtype in {s_STRUCT, s_UNION}

    def isArray(self):
        """
        Tests if the type is an array
        """
        return self.objtype == s_ARRAY

# Standard constants - must be entered first
TypeDef('vx_library_e', s_CONSTANT, defs=dict(VX_LIBRARY_KHR_BASE = 0x0))
TypeDef('vx_vendor_id_e', s_CONSTANT, defs=dict(
    VX_ID_KHRONOS   = 0x000, # The Khronos Group */
    VX_ID_TI        = 0x001, # Texas Instruments, Inc. */
    VX_ID_QUALCOMM  = 0x002, # Qualcomm, Inc. */
    VX_ID_NVIDIA    = 0x003, # NVIDIA Corporation */
    VX_ID_ARM       = 0x004, # ARM Ltd. */
    VX_ID_BDTI      = 0x005, # Berkley Design Technology, Inc. */
    VX_ID_RENESAS   = 0x006, # Renasas Electronics */
    VX_ID_VIVANTE   = 0x007, # Vivante Corporation */
    VX_ID_XILINX    = 0x008, # Xilinx Inc. */
    VX_ID_AXIS      = 0x009, # Axis Communications */
    VX_ID_MOVIDIUS  = 0x00A, # Movidius Ltd. */
    VX_ID_SAMSUNG   = 0x00B, # Samsung Electronics */
    VX_ID_FREESCALE = 0x00C, # Freescale Semiconductor */
    VX_ID_AMD       = 0x00D, # Advanced Micro Devices */
    VX_ID_BROADCOM  = 0x00E, # Broadcom Corporation */
    VX_ID_INTEL     = 0x00F, # Intel Corporation */
    VX_ID_MARVELL   = 0x010, # Marvell Technology Group Ltd. */
    VX_ID_MEDIATEK  = 0x011, # MediaTek, Inc. */
    VX_ID_ST        = 0x012, # STMicroelectronics */
    VX_ID_CEVA      = 0x013, # CEVA DSP */
    VX_ID_ITSEEZ    = 0x014, # Itseez, Inc. */
    VX_ID_IMAGINATION=0x015, # Imagination Technologies */
    VX_ID_NXP       = 0x016, # NXP Semiconductors */
    VX_ID_VIDEANTIS = 0x017, # Videantis */
    VX_ID_SYNOPSYS  = 0x018, # Synopsys */
    VX_ID_CADENCE   = 0x019, # Cadence */
    VX_ID_HUAWEI    = 0x01A, # Huawei */
    VX_ID_SOCIONEXT = 0x01B # Socionext */
    ))
TypeDef(s_vx_type_e, s_CONSTANT, defs=dict(
        VX_TYPE_INVALID = 0,
        VX_TYPE_CHAR = 1,
        VX_TYPE_INT8 = 2,
        VX_TYPE_UINT8 = 3,
        VX_TYPE_INT16 = 4,
        VX_TYPE_UINT16 = 5,
        VX_TYPE_INT32 = 6,
        VX_TYPE_UINT32 = 7,
        VX_TYPE_INT64 = 8,
        VX_TYPE_UINT64 = 9,
        VX_TYPE_FLOAT32 = 10,
        VX_TYPE_FLOAT64 = 11,
        VX_TYPE_ENUM = 12,
        VX_TYPE_SIZE = 13,
        VX_TYPE_DF_IMAGE = 14,
        VX_TYPE_FLOAT16 = 15,
        VX_TYPE_BOOL = 16,

        VX_TYPE_RECTANGLE = 0X20,
        VX_TYPE_KEYPOINT = 0X21,
        VX_TYPE_COORDINATES2D = 0X22,
        VX_TYPE_COORDINATES3D = 0X23,
        VX_TYPE_COORDINATES2DF = 0X24,

        VX_TYPE_NN_CONVOLUTION_PARAMS = 0X25,
		VX_TYPE_NN_DECONVOLUTION_PARAMS = 0X26,
		VX_TYPE_NN_ROI_POOL_PARAMS = 0X27,

        VX_TYPE_HOG_PARAMS = 0X28,
	    VX_TYPE_HOUGH_LINES_PARAMS = 0X29,
	    VX_TYPE_LINE_2D = 0X2A,
	    VX_TYPE_TENSOR_MATRIX_MULTIPLY_PARAMS = 0X2B,

        VX_TYPE_CLASSIFIER_MODEL = 0x2C,

        VX_TYPE_REFERENCE = 0X800,
        VX_TYPE_CONTEXT = 0X801,
        VX_TYPE_GRAPH = 0X802,
        VX_TYPE_NODE = 0X803,
        VX_TYPE_KERNEL = 0X804,
        VX_TYPE_PARAMETER = 0X805,

        VX_TYPE_DELAY = 0X806,
        VX_TYPE_LUT = 0X807,
        VX_TYPE_DISTRIBUTION = 0X808,
        VX_TYPE_PYRAMID = 0X809,
        VX_TYPE_THRESHOLD = 0X80A,
        VX_TYPE_MATRIX = 0X80B,
        VX_TYPE_CONVOLUTION = 0X80C,
        VX_TYPE_SCALAR = 0X80D,
        VX_TYPE_ARRAY = 0X80E,
        VX_TYPE_IMAGE = 0X80F,
        VX_TYPE_REMAP = 0X810,

        VX_TYPE_OBJECT_ARRAY = 0X813,
        VX_TYPE_TENSOR = 0X815))

TypeDef(s_vx_df_image_e, s_CONSTANT, defs=dict(
        VX_DF_IMAGE_VIRT = TypeDef.formatToId('VIRT'),
        VX_DF_IMAGE_RGB = TypeDef.formatToId('RGB2'),
        VX_DF_IMAGE_RGBX = TypeDef.formatToId('RGBA'),
        VX_DF_IMAGE_NV12 = TypeDef.formatToId('NV12'),
        VX_DF_IMAGE_NV21 = TypeDef.formatToId('NV21'),
        VX_DF_IMAGE_UYVY = TypeDef.formatToId('UYVY'),
        VX_DF_IMAGE_YUYV = TypeDef.formatToId('YUYV'),
        VX_DF_IMAGE_IYUV = TypeDef.formatToId('IYUV'),
        VX_DF_IMAGE_YUV4 = TypeDef.formatToId('YUV4'),
        VX_DF_IMAGE_U8 = TypeDef.formatToId('U008'),
        VX_DF_IMAGE_U16 = TypeDef.formatToId('U016'),
        VX_DF_IMAGE_S16 = TypeDef.formatToId('S016'),
        VX_DF_IMAGE_U32 = TypeDef.formatToId('U032'),
        VX_DF_IMAGE_S32 = TypeDef.formatToId('S032')))

TypeDef('vx_enum_e', s_CONSTANT, defs=dict(
    VX_ENUM_DIRECTION                   = 0x00,
    VX_ENUM_ACTION                      = 0x01,
    VX_ENUM_HINT                        = 0x02,
    VX_ENUM_DIRECTIVE                   = 0x03,
    VX_ENUM_INTERPOLATION               = 0x04,
    VX_ENUM_OVERFLOW                    = 0x05,
    VX_ENUM_COLOR_SPACE                 = 0x06,
    VX_ENUM_COLOR_RANGE                 = 0x07,
    VX_ENUM_PARAMETER_STATE             = 0x08,
    VX_ENUM_CHANNEL                     = 0x09,
    VX_ENUM_CONVERT_POLICY              = 0x0A,
    VX_ENUM_THRESHOLD_TYPE              = 0x0B,
    VX_ENUM_BORDER                      = 0x0C,
    VX_ENUM_COMPARISON                  = 0x0D,
    VX_ENUM_MEMORY_TYPE                 = 0x0E,
    VX_ENUM_TERM_CRITERIA               = 0x0F,
    VX_ENUM_NORM_TYPE                   = 0x10,
    VX_ENUM_ACCESSOR                    = 0x11,
    VX_ENUM_ROUND_POLICY                = 0x12,
    VX_ENUM_TARGET                      = 0x13,
    VX_ENUM_BORDER_POLICY               = 0x14,
    VX_ENUM_GRAPH_STATE                 = 0x15,
    VX_ENUM_NONLINEAR                   = 0x16,
    VX_ENUM_PATTERN                     = 0x17,
    VX_ENUM_LBP_FORMAT                  = 0x18,
    VX_ENUM_COMP_METRIC                 = 0x19,
    VX_ENUM_NN_ROUNDING_TYPE            = 0x1A,
    VX_ENUM_NN_POOLING_TYPE	            = 0x1B,
    VX_ENUM_NN_NORMALIZATION_TYPE       = 0x1C,
    VX_ENUM_NN_ACTIVATION_FUNCTION_TYPE = 0x1D,
	VX_ENUM_CLASSIFIER_MODEL            = 0x1E,
    VX_ENUM_IX_USE                      = 0x1F))

# Standard attributes
TypeDef('vx_delay_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_DELAY',
        dict(VX_DELAY_TYPE = 0,
        VX_DELAY_SLOTS = 1))
TypeDef('vx_lut_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_LUT',
        dict(VX_LUT_TYPE = 0,
        VX_LUT_COUNT = 1,
        VX_LUT_SIZE = 2,
        VX_LUT_OFFSET = 3))
TypeDef('vx_pyramid_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_PYRAMID',
        ['VX_PYRAMID_LEVELS',
        'VX_PYRAMID_SCALE',
        'VX_PYRAMID_WIDTH',
        'VX_PYRAMID_HEIGHT',
        'VX_PYRAMID_FORMAT'])
TypeDef('vx_distribution_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_PYRAMID',
        ['VX_DISTRIBUTION_DIMENSIONS',
         'VX_DISTRIBUTION_OFFSET',
         'VX_DISTRIBUTION_RANGE',
         'VX_DISTRIBUTION_BINS',
         'VX_DISTRIBUTION_WINDOW',
         'VX_DISTRIBUTION_SIZE'])
TypeDef('vx_threshold_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_THRESHOLD',
        ['VX_THRESHOLD_TYPE',
        'VX_THRESHOLD_THRESHOLD_VALUE',
        'VX_THRESHOLD_THRESHOLD_LOWER',
        'VX_THRESHOLD_THRESHOLD_UPPER',
        'VX_THRESHOLD_TRUE_VALUE',
        'VX_THRESHOLD_FALSE_VALUE',
        'VX_THRESHOLD_DATA_TYPE',
        'VX_THRESHOLD_INPUT_FORMAT',
        'VX_THRESHOLD_OUTPUT_FORMAT'])
TypeDef('vx_matrix_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_MATRIX',
        ['VX_MATRIX_TYPE',
        'VX_MATRIX_ROWS',
        'VX_MATRIX_COLUMNS',
        'VX_MATRIX_SIZE',
        'VX_MATRIX_ORIGIN',
        'VX_MATRIX_PATTERN'])
TypeDef('vx_convolution_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_CONVOLUTION',
        ['VX_CONVOLUTION_ROWS',
        'VX_CONVOLUTION_COLUMNS',
        'VX_CONVOLUTION_SCALE',
        'VX_CONVOLUTION_SIZE'])
TypeDef('vx_scalar_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_SCALAR',
        ['VX_SCALAR_TYPE'])
TypeDef('vx_array_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_ARRAY',
        ['VX_ARRAY_ITEMTYPE',
        'VX_ARRAY_NUMITEMS',
        'VX_ARRAY_CAPACITY',
        'VX_ARRAY_ITEMSIZE'])
TypeDef('vx_image_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_IMAGE',
        ['VX_IMAGE_WIDTH',
        'VX_IMAGE_HEIGHT',
        'VX_IMAGE_FORMAT',
        'VX_IMAGE_PLANES',
        'VX_IMAGE_SPACE',
        'VX_IMAGE_RANGE',
        'VX_IMAGE_SIZE'
        'VX_IMAGE_MEMORY_TYPE',
        'VX_IMAGE_IS_UNIFORM',
        'VX_IMAGE_UNIFORM_VALUE'])
TypeDef('vx_remap_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_REMAP',
        ['VX_REMAP_SOURCE_WIDTH',
        'VX_REMAP_SOURCE_HEIGHT',
        'VX_REMAP_DESTINATION_WIDTH',
        'VX_REMAP_DESTINATION_HEIGHT'
        ])
TypeDef('vx_object_array_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_OBJECT_ARRAY',
        ['VX_OBJECT_ARRAY_ITEMTYPE',
        'VX_OBJECT_ARRAY_NUMITEMS'])
TypeDef('vx_tensor_attribute_e', s_ATTRIBUTE, s_VX_ID_KHRONOS, 'VX_TYPE_TENSOR',
        ['VX_TENSOR_NUMBER_OF_DIMS',
        'VX_TENSOR_DIMS',
        'VX_TENSOR_DATA_TYPE',
        'VX_TENSOR_FIXED_POINT_POSITION'])

# Standard enumerations
TypeDef('vx_threshold_type_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_THRESHOLD_TYPE',
        ['VX_THRESHOLD_TYPE_BINARY',
        'VX_THRESHOLD_TYPE_RANGE'])
TypeDef('vx_pattern_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_PATTERN',
        ['VX_PATTERN_BOX',
        'VX_PATTERN_CROSS',
        'VX_PATTERN_DISK',
        'VX_PATTERN_OTHER'])
TypeDef('vx_color_space_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_COLOR_SPACE',
        ['VX_COLOR_SPACE_NONE',
        'VX_COLOR_SPACE_BT601_525',
        'VX_COLOR_SPACE_BT601_625',
        'VX_COLOR_SPACE_BT709',
        'VX_COLOR_SPACE_DEFAULT'])
TypeDef('vx_channel_range_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_COLOR_RANGE',
        ['VX_CHANNEL_RANGE_FULL',
        'VX_CHANNEL_RANGE_RESTRICTED'])
TypeDef('vx_memory_type_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_MEMORY_TYPE',
        ['VX_MEMORY_TYPE_NONE',
        'VX_MEMORY_TYPE_HOST'])
TypeDef('vx_convert_policy_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_CONVERT_POLICY',
        ['VX_CONVERT_POLICY_WRAP',
        'VX_CONVERT_POLICY_SATURATE'])
TypeDef('vx_round_policy_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_ROUND_POLICY',
        ['VX_ROUND_POLICY_TO_ZERO',
        'VX_ROUND_POLICY_TO_NEAREST_EVEN'])
TypeDef('vx_termination_criteria_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_TERM_CRITERIA',
        ['VX_TERM_CRITERIA_ITERATIONS',
        'VX_TERM_CRITERIA_EPSILON',
        'VX_TERM_CRITERIA_BOTH'])
TypeDef('vx_non_linear_filter_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_NONLINEAR',
        ['VX_NONLINEAR_FILTER_MEDIAN',
        'VX_NONLINEAR_FILTER_MIN',
        'VX_NONLINEAR_FILTER_MAX'])
TypeDef('vx_interpolation_type_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_INTERPOLATION',
        ['VX_INTERPOLATION_NEAREST_NEIGHBOR',
        'VX_INTERPOLATION_BILINEAR',
        'VX_INTERPOLATION_AREA'])
TypeDef('vx_lbp_format_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_LBP_FORMAT',
        ['VX_LBP',
        'VX_MLBP',
        'VX_ULBP'])
TypeDef('vx_norm_type_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_NORM_TYPE',
        ['VX_NORM_L1', 'VX_NORM_L2'])
TypeDef('vx_channel_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_CHANNEL',
        ['VX_CHANNEL_0',
        'VX_CHANNEL_1',
        'VX_CHANNEL_2',
        'VX_CHANNEL_3',
        'VX_CHANNEL_R',
        'VX_CHANNEL_G',
        'VX_CHANNEL_B',
        'VX_CHANNEL_A',
        'VX_CHANNEL_Y',
        'VX_CHANNEL_U',
        'VX_CHANNEL_V'])
TypeDef('vx_scalar_operation_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_SCALAR_OPERATION',
        ['VX_SCALAR_OP_AND',
        'VX_SCALAR_OP_OR',
        'VX_SCALAR_OP_XOR',
        'VX_SCALAR_OP_NAND',
        'VX_SCALAR_OP_EQUAL',
        'VX_SCALAR_OP_NOTEQUAL',
        'VX_SCALAR_OP_LESS',
        'VX_SCALAR_OP_LESSEQ',
        'VX_SCALAR_OP_GREATER',
        'VX_SCALAR_OP_GREATEREQ',
        'VX_SCALAR_OP_ADD',
        'VX_SCALAR_OP_SUBTRACT',
        'VX_SCALAR_OP_MULTIPLY',
        'VX_SCALAR_OP_DIVIDE',
        'VX_SCALAR_OP_MODULUS',
        'VX_SCALAR_OP_MIN',
        'VX_SCALAR_OP_MAX'])
TypeDef('vx_comp_metric_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_COMP_METRIC',
        ['VX_COMPARE_HAMMING',
        'VX_COMPARE_L1',
        'VX_COMPARE_L2',
        'VX_COMPARE_CCORR',
        'VX_COMPARE_L2_NORM',
        'VX_COMPARE_CCORR_NORM'])
TypeDef('vx_border_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_BORDER',
        ['VX_BORDER_UNDEFINED',
        'VX_BORDER_CONSTANT',
        'VX_BORDER_REPLICATE'])
TypeDef('vx_direction_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_DIRECTION',
        ['VX_INPUT', 'VX_OUTPUT', 'VX_BIDIRECTIONAL'])
TypeDef('vx_hint_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_HINT',
        ['VX_HINT_PERFORMANCE_DEFAULT',
        'VX_HINT_PERFORMANCE_LOW_POWER',
        'VX_HINT_PERFORMANCE_HIGH_SPEED'])
TypeDef('vx_graph_state_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_GRAPH_STATE',
        ['VX_GRAPH_STATE_UNVERIFIED',
        'VX_GRAPH_STATE_VERIFIED',
        'VX_GRAPH_STATE_RUNNING',
        'VX_GRAPH_STATE_ABANDONED',
        'VX_GRAPH_STATE_COMPLETED'])
TypeDef('vx_directive_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_DIRECTIVE',
        ['VX_DIRECTIVE_DISABLE_LOGGING',
        'VX_DIRECTIVE_ENABLE_LOGGING',
        'VX_DIRECTIVE_DISABLE_PERFORMANCE',
        'VX_DIRECTIVE_ENABLE_PERFORMANCE'])
TypeDef('vx_action_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_ACTION',
        ['VX_ACTION_CONTINUE',
        'VX_ACTION_ABANDON'])
TypeDef('vx_ix_use_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_IX_USE',
        ['VX_IX_USE_APPLICATION_CREATE', 'VX_IX_USE_EXPORT_VALUES', 'VX_IX_USE_NO_EXPORT_VALUES'])
TypeDef('vx_nn_rounding_type_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_NN_ROUNDING_TYPE',
        ['VX_NN_DS_SIZE_ROUNDING_FLOOR', 'VX_NN_DS_SIZE_ROUNDING_CEILING'])
TypeDef('vx_nn_pooling_type_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_NN_POOLING_TYPE',
        ['VX_NN_POOLING_MAX', 'VX_NN_POOLING_AVG'])
TypeDef('vx_nn_norm_type_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_NN_NORMALIZATION_TYPE',
        ['VX_NN_NORMALIZATION_SAME_MAP', 'VX_NN_NORMALIZATION_ACROSS_MAPS'])
TypeDef('vx_nn_activation_function_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_NN_ACTIVATION_FUNCTION_TYPE',
        ['VX_NN_ACTIVATION_LOGISTIC', 'VX_NN_ACTIVATION_HYPERBOLIC_TAN', 'VX_NN_ACTIVATION_RELU',
        'VX_NN_ACTIVATION_BRELU', 'VX_NN_ACTIVATION_SOFTRELU', 'VX_NN_ACTIVATION_ABS',
        'VX_NN_ACTIVATION_SQUARE', 'VX_NN_ACTIVATION_SQRT', 'VX_NN_ACTIVATION_LINEAR'])
TypeDef('vx_classifier_model_format_e', s_ENUM, s_VX_ID_KHRONOS, 'VX_ENUM_CLASSIFIER_MODEL',
        ['VX_CLASSIFIER_MODEL_UNDEFINED'])
# Structures and unions
TypeDef('vx_border_t',s_STRUCT, defs=
                    {'mode':'vx_border_e',
                    'constant_value':'vx_pixel_value_t'})
TypeDef('vx_pixel_value_t', s_UNION, defs=
                    {'RGB':'vx_uint8[3]',
                    'RGBX':'vx_uint8[4]',
                    'YUV':'vx_uint8[3]',
                    'U8':s_vx_uint8,
                    'U16':s_vx_uint16,
                    'S16':s_vx_int16,
                    'U32':s_vx_uint32,
                    'S32':s_vx_int32,
                    'reserved':'vx_uint8[16]'})
TypeDef('vx_hog_t',s_STRUCT, defs=
        {'cell_width': s_vx_int32,
        'cell_height': s_vx_int32,
        'block_width': s_vx_int32,
        'block_height': s_vx_int32,
        'block_stride': s_vx_int32,
        'num_bins': s_vx_int32,
        'window_width': s_vx_int32,
        'window_height': s_vx_int32,
        'window_stride': s_vx_int32,
        'threshold': s_vx_float32})
TypeDef('vx_hough_lines_p_t',s_STRUCT, defs=
        {'rho': s_vx_float32,
        'theta': s_vx_float32,
        'threshold': s_vx_int32,
        'line_length': s_vx_int32,
        'line_gap': s_vx_int32,
        'theta_max': s_vx_float32,
        'theta_min': s_vx_float32}),
TypeDef('vx_tensor_matrix_multiply_params_t',s_STRUCT, defs=
        {'transpose_input1': s_vx_bool,
        'transpose_input2': s_vx_bool,
        'transpose_input3': s_vx_bool
        })
TypeDef('vx_keypoint_t', s_STRUCT, defs=dict(
        x = s_vx_int32,
        y = s_vx_int32,
        strength = s_vx_float32,
        scale = s_vx_float32,
        orientation = s_vx_float32,
        tracking_status = s_vx_int32,
        error = s_vx_float32))
TypeDef('vx_line2d_t', s_STRUCT, defs=dict(
        start_x = s_vx_float32,
        start_y = s_vx_float32,
        end_x = s_vx_float32,
        end_y = s_vx_float32))
TypeDef('vx_rectangle_t', s_STRUCT, defs=dict(
        start_x = s_vx_uint32,
        start_y = s_vx_uint32,
        end_x = s_vx_uint32,
        end_y = s_vx_uint32))
TypeDef('vx_coordinates2d_t', s_STRUCT, defs=dict(
        x = s_vx_uint32,
        y = s_vx_uint32))
TypeDef('vx_coordinates2df_t', s_STRUCT, defs=dict(
        x = s_vx_float32,
        y = s_vx_float32))
TypeDef('vx_coordinates3d_t', s_STRUCT, defs=dict(
        x = s_vx_uint32,
        y = s_vx_uint32,
        z = s_vx_uint32))
TypeDef('vx_nn_convolution_params_t', s_STRUCT, defs=dict(
        padding_x = s_vx_size,
        padding_y = s_vx_size,
        overflow_policy = 'vx_convert_policy_e',
        rounding_policy = 'vx_round_policy_e',
        down_scale_size_rounding = 'vx_nn_rounding_type_e',
        dilation_x = s_vx_size,
        dilation_y = s_vx_size))
TypeDef('vx_nn_deconvolution_params_t', s_STRUCT, defs=dict(
        padding_x = s_vx_size,
        padding_y = s_vx_size,
        overflow_policy = 'vx_convert_policy_e',
        rounding_policy = 'vx_round_policy_e',
        a_x = s_vx_size,
        a_y = s_vx_size))
TypeDef('vx_nn_roi_pool_params_t', s_STRUCT, defs=dict(
        pool_type = 'vx_nn_pooling_type_e'))
# Arrays
TypeDef('vx_uint8[3]', s_ARRAY, size=3, eltype=s_vx_uint8)
TypeDef('vx_uint8[4]', s_ARRAY, size=4, eltype=s_vx_uint8)
TypeDef('vx_uint8[16]', s_ARRAY, size=16, eltype=s_vx_uint8)

# Inherent types with their default values
TypeDef(s_vx_size, s_INHERENT, eltype="0")
TypeDef(s_vx_char, s_INHERENT, eltype='.')
TypeDef(s_vx_bool, s_INHERENT, eltype="false")
TypeDef(s_vx_int8, s_INHERENT, eltype="0")
TypeDef(s_vx_uint8, s_INHERENT, eltype="0")
TypeDef(s_vx_int16, s_INHERENT, eltype="0")
TypeDef(s_vx_uint16, s_INHERENT, eltype="0")
TypeDef(s_vx_int32, s_INHERENT, eltype="0")
TypeDef(s_vx_uint32, s_INHERENT, eltype="0")
TypeDef(s_vx_int64, s_INHERENT, eltype="0")
TypeDef(s_vx_uint64, s_INHERENT, eltype="0")
TypeDef(s_vx_float16, s_INHERENT, eltype="0.0")
TypeDef(s_vx_float32, s_INHERENT, eltype="0.0")
TypeDef(s_vx_float64, s_INHERENT, eltype="0.0")
TypeDef('vx_invalid', s_INHERENT, eltype="")
TypeDef(s_vx_df_image, s_INHERENT, eltype="U008")

# Opaque types
TypeDef('vx_classifier_model', s_OPAQUE)  # we know nothing about this

# Base types - first, vx_enum. We assign arbitrary indices to all enumerated types,
# and include 'vx_uint32' as the first item, after sorting the rest.
newlist = []
for name, obj in list(TypeDef.typeDict.items()):
    if name.endswith("_e"):
        newlist.append(name)
newlist.sort()
newlist.insert(0, s_vx_uint32)
TypeDef(s_vx_enum, s_BASE, defs=newlist)
del newlist

# Now the vx_reference objects
TypeDef(s_vx_reference, s_BASE, defs={
    s_vx_delay: 'VX_TYPE_DELAY',
    s_vx_lut: 'VX_TYPE_LUT',
    s_vx_distribution: 'VX_TYPE_DISTRIBUTION',
    s_vx_pyramid: 'VX_TYPE_PYRAMID',
    s_vx_threshold: 'VX_TYPE_THRESHOLD',
    s_vx_matrix: 'VX_TYPE_MATRIX',
    s_vx_convolution: 'VX_TYPE_CONVOUTION',
    s_vx_scalar: 'VX_TYPE_SCALAR',
    s_vx_array: 'VX_TYPE_ARRAY',
    s_vx_image: 'VX_TYPE_IMAGE',
    s_vx_remap: 'VX_TYPE_REMAP',
    s_vx_object_array: 'VX_TYPE_OBJECT_ARRAY',
    s_vx_tensor: 'VX_TYPE_TENSOR'})

# and the standard object reference types
# Probably this is not a lot of use for this
# application? Might be more useful to map them
# to the XML tags and remove that mapping from the
# xml module and where it appears randomly.
TypeDef(s_vx_delay, s_OBJECT,
        defs={'VX_DELAY_TYPE': s_vx_type_e,
         'VX_DELAY_SLOTS': s_vx_size},
         attrs=dict(elemtype='VX_DELAY_TYPE',
        numelems='VX_DELAY_SLOTS'))
TypeDef(s_vx_lut, s_OBJECT, defs=
        {'VX_LUT_TYPE': s_vx_type_e,
         'VX_LUT_COUNT': s_vx_size,
         'VX_LUT_SIZE': s_vx_size,
         'VX_LUT_OFFSET': s_vx_uint32},
        attrs=dict(elemtype='VX_LUT_TYPE',  numelems='VX_LUT_COUNT'))
TypeDef(s_vx_distribution, s_OBJECT, defs=
        {'VX_DISTRIBUTION_DIMENSIONS': s_vx_size,
        'VX_DISTRIBUTION_OFFSET': s_vx_int32,
        'VX_DISTRIBUTION_RANGE': s_vx_uint32,
        'VX_DISTRIBUTION_BINS': s_vx_size,
        'VX_DISTRIBUTION_WINDOW': s_vx_uint32,
        'VX_DISTRIBUTION_SIZE': s_vx_size},
        attrs=dict(elemtype=s_vx_uint32,  numelems='VX_DISTRIBUTION_BINS'))
TypeDef(s_vx_pyramid, s_OBJECT, defs=
        {'VX_PYRAMID_LEVELS': s_vx_size,
        'VX_PYRAMID_SCALE': s_vx_float32,
        'VX_PYRAMID_WIDTH': s_vx_uint32,
        'VX_PYRAMID_HEIGHT': s_vx_uint32,
        'VX_PYRAMID_FORMAT': s_vx_df_image},
        attrs=dict(elemtype=s_vx_image,  numelems='VX_PYRAMID_LEVELS'))
TypeDef(s_vx_threshold, s_OBJECT, defs=
        {'VX_THRESHOLD_TYPE': 'vx_threshold_type_e',
        'VX_THRESHOLD_INPUT_FORMAT': s_vx_df_image_e,
        'VX_THRESHOLD_OUTPUT_FORMAT': s_vx_df_image_e})
TypeDef(s_vx_matrix, s_OBJECT, defs=
        {'VX_MATRIX_TYPE': s_vx_type_e,
        'VX_MATRIX_ROWS': s_vx_size,
        'VX_MATRIX_COLUMNS': s_vx_size,
        'VX_MATRIX_SIZE': s_vx_size,
        'VX_MATRIX_ORIGIN': 'vx_coordinates2d_t',
        'VX_MATRIX_PATTERN': 'vx_pattern_e'})
TypeDef(s_vx_convolution, s_OBJECT, defs=
        {'VX_CONVOLUTION_ROWS': s_vx_size,
        'VX_CONVOLUTION_COLUMNS': s_vx_size,
        'VX_CONVOLUTION_SCALE': s_vx_uint32,
        'VX_CONVOLUTION_SIZE': s_vx_size})
TypeDef(s_vx_scalar, s_OBJECT, defs={'VX_SCALAR_TYPE': s_vx_type_e})
TypeDef(s_vx_array, s_OBJECT, defs=
        {'VX_ARRAY_ITEMTYPE': s_vx_type_e,
        'VX_ARRAY_NUMITEMS': s_vx_size ,
        'VX_ARRAY_CAPACITY': s_vx_size,
        'VX_ARRAY_ITEMSIZE': s_vx_size},
        attrs=dict(elemtype='VX_ARRAY_ITEMTYPE',  numelems='VX_ARRAY_NUMITEMS'))
TypeDef(s_vx_image, s_OBJECT, defs=
        {'VX_IMAGE_WIDTH': s_vx_uint32,
        'VX_IMAGE_HEIGHT': s_vx_uint32,
        'VX_IMAGE_FORMAT': s_vx_df_image,
        'VX_IMAGE_PLANES': s_vx_size,
        'VX_IMAGE_SPACE': 'vx_color_space_e',
        'VX_IMAGE_RANGE': 'vx_channel_range_e',
        'VX_IMAGE_MEMORY_TYPE': 'vx_memory_type_e',
        'VX_IMAGE_IS_UNIFORM': s_vx_bool,
        'VX_IMAGE_UNIFORM_VALUE': 'vx_pixel_value_t'})
TypeDef(s_vx_remap, s_OBJECT, defs=
        {'VX_REMAP_SOURCE_WIDTH': s_vx_uint32,
        'VX_REMAP_SOURCE_HEIGHT': s_vx_uint32,
        'VX_REMAP_DESTINATION_WIDTH': s_vx_uint32,
        'VX_REMAP_DESTINATION_HEIGHT': s_vx_uint32
        })
TypeDef(s_vx_object_array, s_OBJECT, defs=
        {'VX_OBJECT_ARRAY_ITEMTYPE': s_vx_type_e,
        'VX_OBJECT_ARRAY_NUMITEMS':s_vx_size})
TypeDef(s_vx_tensor, s_OBJECT, defs=
        {'VX_TENSOR_NUMBER_OF_DIMS': s_vx_size,
        'VX_TENSOR_DIMS': 'vx_size *',
        'VX_TENSOR_DATA_TYPE': s_vx_type_e,
        'VX_TENSOR_FIXED_POINT_POSITION': s_vx_int8})


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('people', '0002_auto_20161122_0857'),
    ]

    operations = [
        migrations.AddField(
            model_name='user',
            name='first_name',
            field=models.CharField(max_length=30, verbose_name='Nombre', blank=True),
        ),
        migrations.AddField(
            model_name='user',
            name='last_name',
            field=models.CharField(max_length=30, verbose_name='Apellido', blank=True),
        ),
    ]


from django.conf.urls import url

from . import views

urlpatterns = [
    # 客户资料新增
    url(r'^add/$', views.add, name='add'),
    # 客户资料更新
    url(r'^update/$', views.update, name='update'),
    # 客户资料 ID 查询
    url(r'^get/(.+)/$', views.get_by_id, name='get_by_id'),
    # 客户资料 删除
    url(r'^delete/(.+)/$', views.delete, name='delete'),
    # 提交pcb 资料 审核
    url(r'^check/(.+)/$', views.check, name='check'),
    # 客户上传PCB文件
    url(r'^pcb_upload/$', views.pcb_upload, name='pcb_upload'),
    # 客户下载PCB文件
    url(r'^pcb_down/(.+)/$', views.pcb_down, name='pcb_down'),
    # 客户资料 customer_id 查询 （查询客户自己的资料信息）
    url(r'^list/$', views.list_by_customer_id, name='list_by_customer_id'),
    # 分页查询客户资料
    url(r'^list/(\d+)/(\d+)/$', views.list, name='list'),
    # 查询 PCB 资料 下单 信息
    url(r'^get_order_info/(.+)/$', views.get__order_info, name='get__order_info'),
]


# Local libraries
import input_reader
import interpreter
import physics

# Third-party libraries
from numpy import genfromtxt, nan, array

INPUT_FILE_NAME = "config.in"

def main():
    input_dict = input_reader.create_input_dictionary(INPUT_FILE_NAME)
    properties_dict = input_reader.create_properties_dictionary(input_dict[0]["ADSORBATE_DATA_FILE"])
    data_dict = {}

    for index in input_dict:
        data_dict[index] = {}
        properties_dict = input_reader.create_properties_dictionary(input_dict[index]["ADSORBATE_DATA_FILE"])

        file_data = genfromtxt(fname=input_dict[index]["DATA_FILES"], filling_values=nan)
        if input_dict[index]["DATA_TYPES"] == "isotherm":
            data_dict[index]["temperature"] = input_dict[index]["TEMPERATURES"]
            data_dict[index]["pressure"] = file_data[:, 0] * interpreter.convert_input(
                                                                    unit=input_dict[index]["PRESSURE_UNITS"],
                                                                    molecular_mass=properties_dict["MOLECULAR_MASS"])

            data_dict[index]["loading"] = file_data[:, 1] * interpreter.convert_input(
                                                                    unit=input_dict[index]["LOADING_UNITS"],
                                                                    molecular_mass=properties_dict["MOLECULAR_MASS"])
        elif input_dict[index]["DATA_TYPES"] == "isobar":
            data_dict[index]["pressure"] = input_dict[index]["PRESSURES"]
            data_dict[index]["temperature"] = file_data[:, 0] * interpreter.convert_input(
                                                                    unit=input_dict[index]["TEMPERATURE_UNITS"],
                                                                    molecular_mass=properties_dict["MOLECULAR_MASS"])

            data_dict[index]["loading"] = file_data[:, 1] * interpreter.convert_input(
                                                                    unit=input_dict[index]["LOADING_UNITS"],
                                                                    molecular_mass=properties_dict["MOLECULAR_MASS"])
        elif input_dict[index]["DATA_TYPES"] == "characteristic":
            data_dict[index]["potential"] = file_data[:, 0] * interpreter.convert_input(
                                                                    unit=input_dict[index]["POTENTIAL_UNITS"],
                                                                    molecular_mass=properties_dict["MOLECULAR_MASS"])

            data_dict[index]["volume"] = file_data[:, 1] * interpreter.convert_input(
                                                                    unit=input_dict[index]["VOLUME_UNITS"],
                                                                    molecular_mass=properties_dict["MOLECULAR_MASS"])
        else:
            raise ValueError(f"{input_dict[index]['DATA_TYPES']} is not a recognized argument!")

    if input_dict[0]["PLOT_DATA"] == "yes":
        interpreter.plot_data(source_dictionary=data_dict, input_dictionary=input_dict,
                              plot_format=input_dict[0]["DATA_TYPES"], save=input_dict[0]["SAVE_DATA_PLOT"])

    if input_dict[0]["COMPUTE_ENTHALPY"] == "yes":
        enthalpy = interpreter.compute_adsorption_enthalpy(data_dictionary=data_dict, input_dictionary=input_dict)

        if input_dict[0]["PLOT_ENTHALPY"] == "yes":
            interpreter.plot_data(source_dictionary=enthalpy, input_dictionary=input_dict,
                                  plot_format="enthalpy", save=input_dict[0]["SAVE_ENTHALPY_PLOT"])

        if input_dict[0]["SAVE_ENTHALPY_DATA"] == "yes":
            interpreter.write_data(source_dictionary=enthalpy, input_dictionary=input_dict,
                                   properties_dictionary=properties_dict, write_format="enthalpy")

    if input_dict[0]["COMPUTE_CHARACTERISTIC_CURVE"] == "yes":

        for index in data_dict:
            if input_dict[index]["DATA_TYPES"] == "isotherm":
                data_dict[index]["saturation_pressure"] = interpreter.compute_saturation_pressure_from_method(
                                                method=input_dict[index]["ADSORBATE_SATURATION_PRESSURE"],
                                                temperature=data_dict[index]["temperature"],
                                                properties_dictionary=properties_dict,
                                                saturation_pressure_file=input_dict[index]["SATURATION_PRESSURE_FILE"])

                data_dict[index]["density"] = interpreter.compute_density_from_method(
                                                                        method=input_dict[index]["ADSORBATE_DENSITY"],
                                                                        temperature=data_dict[index]["temperature"],
                                                                        properties_dictionary=properties_dict)

            elif input_dict[index]["DATA_TYPES"] == "isobar":
                saturation_pressure_array = []
                density_array = []
                for temperature in data_dict[index]["temperature"]:
                    saturation_pressure_array.append(interpreter.compute_saturation_pressure_from_method(
                                                method=input_dict[index]["ADSORBATE_SATURATION_PRESSURE"],
                                                temperature=temperature,
                                                properties_dictionary=properties_dict,
                                                saturation_pressure_file=input_dict[index]["SATURATION_PRESSURE_FILE"]))

                    density_array.append(interpreter.compute_density_from_method(
                                                method=input_dict[index]["ADSORBATE_DENSITY"],
                                                temperature=temperature,
                                                properties_dictionary=properties_dict))

                data_dict[index]["saturation_pressure"] = array(saturation_pressure_array)
                data_dict[index]["density"] = array(density_array)

            elif input_dict[index]["DATA_TYPE"] == "characteristic":
                continue

            data_dict[index]["potential"] = physics.get_adsorption_potential(
                                                            temperature=data_dict[index]["temperature"],
                                                            saturation_pressure=data_dict[index]["saturation_pressure"],
                                                            pressure=data_dict[index]["pressure"])

            data_dict[index]["volume"] = physics.get_adsorption_volume(
                                                            adsorbed_amount=data_dict[index]["loading"],
                                                            adsorbate_density=data_dict[index]["density"])

        if input_dict[0]["SAVE_CHARACTERISTIC_CURVE_DATA"] == "yes":
            interpreter.write_data(source_dictionary=data_dict, input_dictionary=input_dict,
                                   properties_dictionary=properties_dict, write_format="characteristic")

        if input_dict[0]["PLOT_CHARACTERISTIC_CURVE"] == "yes":
            interpreter.plot_data(source_dictionary=data_dict, input_dictionary=input_dict,
                                  plot_format="characteristic", save=input_dict[0]["SAVE_CHARACTERISTIC_CURVE_PLOT"])

    if input_dict[0]["PREDICT_ISOTHERMS"] == "yes":
        predicted_isotherms = interpreter.predict_data(data_dictionary=data_dict,
                                                       input_dictionary=input_dict,
                                                       prediction_type="isotherm",
                                                       properties_dictionary=properties_dict)

        if input_dict[0]["PLOT_PREDICTED_ISOTHERMS"] == "yes":
            interpreter.plot_data(source_dictionary=predicted_isotherms, input_dictionary=input_dict,
                                  plot_format="isotherm", save=input_dict[0]["SAVE_PREDICTED_ISOTHERMS_PLOT"])

        if input_dict[0]["SAVE_PREDICTED_ISOTHERMS_DATA"] == "yes":
            interpreter.write_data(source_dictionary=predicted_isotherms, input_dictionary=input_dict,
                                   properties_dictionary=properties_dict, write_format="isotherm")

    if input_dict[0]["PREDICT_ISOBARS"] == "yes":
        predicted_isobars = interpreter.predict_data(data_dictionary=data_dict,
                                                     input_dictionary=input_dict,
                                                     prediction_type="isobar",
                                                     properties_dictionary=properties_dict)

        if input_dict[0]["PLOT_PREDICTED_ISOBARS"] == "yes":
            interpreter.plot_data(source_dictionary=predicted_isobars, input_dictionary=input_dict,
                                  plot_format="isobar", save=input_dict[0]["SAVE_PREDICTED_ISOBARS_PLOT"])

        if input_dict[0]["SAVE_PREDICTED_ISOBARS_DATA"] == "yes":
            interpreter.write_data(source_dictionary=predicted_isobars, input_dictionary=input_dict,
                                   properties_dictionary=properties_dict, write_format="isobar")

    if input_dict[0]["PREDICT_ISOSTERES"] == "yes":
        predicted_isosteres = interpreter.predict_data(data_dictionary=data_dict,
                                                       input_dictionary=input_dict,
                                                       prediction_type="isostere",
                                                       properties_dictionary=properties_dict)

        if input_dict[0]["PLOT_PREDICTED_ISOSTERES"] == "yes":
            interpreter.plot_data(source_dictionary=predicted_isosteres, input_dictionary=input_dict,
                                  plot_format="isostere", save=input_dict[0]["SAVE_PREDICTED_ISOSTERES_PLOT"])

        if input_dict[0]["SAVE_PREDICTED_ISOSTERES_DATA"] == "yes":
            interpreter.write_data(source_dictionary=predicted_isosteres, input_dictionary=input_dict,
                                   properties_dictionary=properties_dict, write_format="isostere")

    if input_dict[0]["SHOW_PLOTS"] == "yes":
        interpreter.show_plots()

if __name__ == "__main__":
    main()


# 두 명의 새로운 사람이 오는 버전

n = int(input()) # 좌석의 개수 : n개
seats = input() # 0 : 공석, 1 : 자리 있음

seats = list(seats) # 문자열에서 list로 변환

max_min = 0
# 두 명을 비워진 자리에 집어 넣었을 때의 모든 경우 조회
for i in range(n):

    if seats[i] == '1': # 자리 있을 경우 그 다음 자리 조회
            continue

    seats[i] = '1' # 새로 온 한 명 자리에 앉기

    for j in range(i+1, n):

        if seats[j] == '1': # 자리 있을 경우 그 다음 자리 조회
            continue

        seats[j] = '1' # 새로 온 나머지 한 명 자리에 앉기

        dis_arr = []
        for k in range(n):
            # 새로 온 두 명 포함한 모든 좌석 조회
            # 사람 간 거리 중 최솟값 구하기

            if seats[k] == '1': # 사람 있는 자리의 index 모두 dis_arr 배열에 append
                dis_arr.append(k)

        # print(dis_arr)
        min_dis = n # 사람 간 거리 중 최솟값 구하기 위한 변수
        length = len(dis_arr)

        for l in range(length - 1): # 사람 간 거리 구한 후, 그 중 최솟값 구하기
            dis = dis_arr[l+1] - dis_arr[l]

            min_dis = min(min_dis, dis)

        # print(min_dis)
        max_min = max(max_min, min_dis)

        seats[j] = 0 # j가 앉았던 자리 다시 공석으로 초기화
    seats[i] = 0 # i가 앉았던 자리 다시 공석으로 초기화

print(max_min)


# coding: utf-8

# In[ ]:

import numpy as np
import json
import nltk
import scipy.spatial.distance as distance

# In[ ]:

def get_json(filename):
    with open(filename, 'r', encoding = 'utf-8') as f:
        return json.load(f)

# In[ ]:

def get_distances(embeddings):
    similarities = {}
    for course in embeddings:
        similarities[course] = {}
        for lecture in embeddings[course]:
            similarities[course][lecture] = {}
            for slide in embeddings[course][lecture]:
                similarities[course][lecture][slide] = {}
                for content in embeddings[course][lecture][slide]:
                    similarities[course][lecture][slide][content] = {}
                    for course2 in embeddings:
                        for lecture2 in embeddings[course2]:
                            for slide2 in embeddings[course2][lecture2]:
                                if content in embeddings[course2][lecture2][slide2]:
                                    similarities[course][lecture][slide][content][course2 + '##' + lecture2 + '##' + slide2] = distance.cosine(
                                        embeddings[course][lecture][slide][content], embeddings[course2][lecture2][slide2][content])
                                    if len(similarities[course][lecture][slide][content]) > 10:
                                        del similarities[course][lecture][slide][content][min(d, key=d.get)]
    return similarities

# In[ ]:

def write_to_json(filename, data):
    with open(filename + '.json', 'w', encoding = 'utf-8') as outfile:
        json.dump(data, outfile)

# In[ ]:

def make_json_readable(jsonname, filename):
    data = {}
    with open(jsonname, 'r', encoding = 'utf-8') as f:
        data = json.load(f)
    with open(filename, 'w', encoding = 'utf-8') as f:
        f.write('')
    with open(filename, 'a', encoding = 'utf-8') as f:
        for course in data:
            f.write(course + '{\n')
            for lecture in data[course]:
                f.write('\t' + lecture + '{\n')
                for slide in data[course][lecture]:
                    f.write('\t\t' + slide + '{\n')
                    for content in data[course][lecture][slide]:
                        f.write('\t\t\t' + content + ':' + str(np.array(data[course][lecture][slide][content])) + '\n')
                    f.write('\t\t}\n')
                f.write('\t}\n')
            f.write('}\n')

# In[ ]:

def main():
    embeddings = get_json('multiplicative_embeddings.json')
    similarities = get_distances(embeddings)
    write_to_json('multiplicative_embeddings_analysis.json')
    make_json_readable('multiplicative_embeddings_analysis.json', 'multiplicative_embeddings_analysis_readable.txt')

# In[ ]:

main()


import traceback
from typing import Callable, Iterable, List, Optional, Tuple, Type, Union

from fastapi import Request
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError, StarletteHTTPException
from lark.exceptions import VisitError
from pydantic import ValidationError

from optimade.exceptions import BadRequest, OptimadeHTTPException
from optimade.models import ErrorResponse, ErrorSource, OptimadeError
from optimade.server.config import CONFIG
from optimade.server.logger import LOGGER
from optimade.server.routers.utils import JSONAPIResponse, meta_values

def general_exception(
    request: Request,
    exc: Exception,
    status_code: int = 500,  # A status_code in `exc` will take precedence
    errors: Optional[List[OptimadeError]] = None,
) -> JSONAPIResponse:
    """Handle an exception

    Parameters:
        request: The HTTP request resulting in the exception being raised.
        exc: The exception being raised.
        status_code: The returned HTTP status code for the error response.
        errors: List of error resources as defined in
            [the OPTIMADE specification](https://github.com/Materials-Consortia/OPTIMADE/blob/develop/optimade.rst#json-response-schema-common-fields).

    Returns:
        A JSON HTTP response based on [`ErrorResponse`][optimade.models.responses.ErrorResponse].

    """
    debug_info = {}
    if CONFIG.debug:
        tb = "".join(
            traceback.format_exception(type(exc), value=exc, tb=exc.__traceback__)
        )
        LOGGER.error("Traceback:\n%s", tb)
        debug_info[f"_{CONFIG.provider.prefix}_traceback"] = tb

    try:
        http_response_code = int(exc.status_code)  # type: ignore[attr-defined]
    except AttributeError:
        http_response_code = int(status_code)

    try:
        title = str(exc.title)  # type: ignore[attr-defined]
    except AttributeError:
        title = str(exc.__class__.__name__)

    try:
        detail = str(exc.detail)  # type: ignore[attr-defined]
    except AttributeError:
        detail = str(exc)

    if errors is None:
        errors = [OptimadeError(detail=detail, status=http_response_code, title=title)]

    response = ErrorResponse(
        meta=meta_values(
            url=request.url,
            data_returned=0,
            data_available=0,
            more_data_available=False,
            schema=CONFIG.schema_url,
            **debug_info,
        ),
        errors=errors,
    )

    return JSONAPIResponse(
        status_code=http_response_code,
        content=jsonable_encoder(response, exclude_unset=True),
    )

def http_exception_handler(
    request: Request,
    exc: Union[StarletteHTTPException, OptimadeHTTPException],
) -> JSONAPIResponse:
    """Handle a general HTTP Exception from Starlette

    Parameters:
        request: The HTTP request resulting in the exception being raised.
        exc: The exception being raised.

    Returns:
        A JSON HTTP response through [`general_exception()`][optimade.server.exception_handlers.general_exception].

    """
    return general_exception(request, exc)

def request_validation_exception_handler(
    request: Request, exc: RequestValidationError
) -> JSONAPIResponse:
    """Handle a request validation error from FastAPI

    `RequestValidationError` is a specialization of a general pydantic `ValidationError`.
    Pass-through directly to [`general_exception()`][optimade.server.exception_handlers.general_exception].

    Parameters:
        request: The HTTP request resulting in the exception being raised.
        exc: The exception being raised.

    Returns:
        A JSON HTTP response through [`general_exception()`][optimade.server.exception_handlers.general_exception].

    """
    return general_exception(request, exc)

def validation_exception_handler(
    request: Request, exc: ValidationError
) -> JSONAPIResponse:
    """Handle a general pydantic validation error

    The pydantic `ValidationError` usually contains a list of errors,
    this function extracts them and wraps them in the OPTIMADE specific error resource.

    Parameters:
        request: The HTTP request resulting in the exception being raised.
        exc: The exception being raised.

    Returns:
        A JSON HTTP response through [`general_exception()`][optimade.server.exception_handlers.general_exception].

    """
    status = 500
    title = "ValidationError"
    errors = set()
    for error in exc.errors():
        pointer = "/" + "/".join([str(_) for _ in error["loc"]])
        source = ErrorSource(pointer=pointer)
        code = error["type"]
        detail = error["msg"]
        errors.add(
            OptimadeError(
                detail=detail, status=status, title=title, source=source, code=code
            )
        )
    return general_exception(request, exc, status_code=status, errors=list(errors))

def grammar_not_implemented_handler(
    request: Request, exc: VisitError
) -> JSONAPIResponse:
    """Handle an error raised by Lark during filter transformation

    All errors raised during filter transformation are wrapped in the Lark `VisitError`.
    According to the OPTIMADE specification, these errors are repurposed to be 501 NotImplementedErrors.

    For special exceptions, like [`BadRequest`][optimade.exceptions.BadRequest], we pass-through to
    [`general_exception()`][optimade.server.exception_handlers.general_exception], since they should not
    return a 501 NotImplementedError.

    Parameters:
        request: The HTTP request resulting in the exception being raised.
        exc: The exception being raised.

    Returns:
        A JSON HTTP response through [`general_exception()`][optimade.server.exception_handlers.general_exception].

    """
    pass_through_exceptions = (BadRequest,)

    orig_exc = getattr(exc, "orig_exc", None)
    if isinstance(orig_exc, pass_through_exceptions):
        return general_exception(request, orig_exc)

    rule = getattr(exc.obj, "data", getattr(exc.obj, "type", str(exc)))

    status = 501
    title = "NotImplementedError"
    detail = (
        f"Error trying to process rule '{rule}'"
        if not str(exc.orig_exc)
        else str(exc.orig_exc)
    )
    error = OptimadeError(detail=detail, status=status, title=title)
    return general_exception(request, exc, status_code=status, errors=[error])

def not_implemented_handler(
    request: Request, exc: NotImplementedError
) -> JSONAPIResponse:
    """Handle a standard NotImplementedError Python exception

    Parameters:
        request: The HTTP request resulting in the exception being raised.
        exc: The exception being raised.

    Returns:
        A JSON HTTP response through [`general_exception()`][optimade.server.exception_handlers.general_exception].

    """
    status = 501
    title = "NotImplementedError"
    detail = str(exc)
    error = OptimadeError(detail=detail, status=status, title=title)
    return general_exception(request, exc, status_code=status, errors=[error])

def general_exception_handler(request: Request, exc: Exception) -> JSONAPIResponse:
    """Catch all Python Exceptions not handled by other exception handlers

    Pass-through directly to [`general_exception()`][optimade.server.exception_handlers.general_exception].

    Parameters:
        request: The HTTP request resulting in the exception being raised.
        exc: The exception being raised.

    Returns:
        A JSON HTTP response through [`general_exception()`][optimade.server.exception_handlers.general_exception].

    """
    return general_exception(request, exc)

OPTIMADE_EXCEPTIONS: Iterable[
    Tuple[
        Type[Exception],
        Callable[[Request, Exception], JSONAPIResponse],
    ]
] = [
    (StarletteHTTPException, http_exception_handler),
    (OptimadeHTTPException, http_exception_handler),
    (RequestValidationError, request_validation_exception_handler),
    (ValidationError, validation_exception_handler),
    (VisitError, grammar_not_implemented_handler),
    (NotImplementedError, not_implemented_handler),  # type: ignore[list-item] # not entirely sure why this entry triggers mypy
    (Exception, general_exception_handler),
]
"""A tuple of all pairs of exceptions and handler functions that allow for
appropriate responses to be returned in certain scenarios according to the
OPTIMADE specification.

To use these in FastAPI app code:

```python
from fastapi import FastAPI
app = FastAPI()
for exception, handler in OPTIMADE_EXCEPTIONS:
    app.add_exception_handler(exception, handler)
```

"""


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

# ## Solar Radiation Prediction
#
# > meteorological data from the HI-SEAS weather station from four months (September through December 2016) between Mission IV and Mission V.
#
# Units:
#
# * Solar radiation: watts per meter^2
# * Temperature: degrees Fahrenheit
# * Humidity: percent
# * Barometric pressure: Hg
# * Wind direction: degrees
# * Wind speed: miles per hour
# * Sunrise/sunset: Hawaii time

# ### Useful imports and read the data

# In[ ]:

import time
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from datetime import datetime
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestRegressor
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
import xgboost as xgb

# In[ ]:

# Read the data
df = pd.read_csv("../../../input/dronio_SolarEnergy/SolarPrediction.csv", parse_dates=['Data'])
df.head()

# In[ ]:

df.describe()

# ### Feature Engineering

# In[ ]:

# Convert all dates and times to unix timestamp (timezone doesn't matter now)
df['Data'] = df['Data'].dt.date.astype(str)
df['TimeSunRise'] = df['Data'] + ' ' + df['TimeSunRise']
df['TimeSunSet'] = df['Data'] + ' ' + df['TimeSunSet']
df['Data'] = df['Data'] + ' ' + df['Time']

# Convert to Unix timestamp
fields = ['Data', 'TimeSunRise', 'TimeSunSet']
for x in fields:
    df[x + '_UnixTimeStamp'] = df[x].apply( lambda k: int(datetime.strptime(k, "%Y-%m-%d %H:%M:%S").timestamp()) )

# New sun time field
df['SunTime'] = df['TimeSunSet_UnixTimeStamp'] - df['TimeSunRise_UnixTimeStamp']

# Drop old columns
df.drop('UNIXTime', axis=1, inplace=True)
df.drop('Data', axis=1, inplace=True)
df.drop('Time', axis=1, inplace=True)
df.drop('TimeSunRise', axis=1, inplace=True)
df.drop('TimeSunSet', axis=1, inplace=True)

# ### Visualization

# In[ ]:

def dddraw(X_reduced,name):
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    # To getter a better understanding of interaction of the dimensions
    # plot the first three PCA dimensions
    fig = plt.figure(1, figsize=(8, 6))
    ax = Axes3D(fig, elev=-150, azim=110)
    ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=Y,cmap=plt.cm.Paired)
    titel="First three directions of "+name
    ax.set_title(titel)
    ax.set_xlabel("1st eigenvector")
    ax.w_xaxis.set_ticklabels([])
    ax.set_ylabel("2nd eigenvector")
    ax.w_yaxis.set_ticklabels([])
    ax.set_zlabel("3rd eigenvector")
    ax.w_zaxis.set_ticklabels([])

    print()

# In[ ]:

from sklearn.decomposition import PCA, FastICA,SparsePCA,NMF, LatentDirichletAllocation,FactorAnalysis
from sklearn.random_projection import GaussianRandomProjection,SparseRandomProjection
from sklearn.cluster import KMeans,Birch
import statsmodels.formula.api as sm
from scipy import linalg
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler,PolynomialFeatures
import matplotlib.pyplot as plt

n_col=12
X = df.drop('Radiation',axis=1) # we only take the first two features.

def rmsle(y_predicted, y_real):
    return np.sqrt(np.mean(np.power(np.log1p(y_predicted)-np.log1p(y_real), 2)))
def procenterror(y_predicted, y_real):
     return np.round( np.mean(np.abs(y_predicted-y_real) )/ np.mean(y_real) *100 ,1)

Y=df['Radiation']
scaler = MinMaxScaler()
scaler.fit(X)
X=scaler.transform(X)
poly = PolynomialFeatures(2)
X=poly.fit_transform(X)

names = [ 'PCA', 'FastICA', 'Gauss', 'KMeans',    'NMF',  ]

classifiers = [  PCA(n_components=n_col), FastICA(n_components=n_col), GaussianRandomProjection(n_components=3), KMeans(n_clusters=24),    NMF(n_components=n_col),   ]
correction= [1,1,0,0,0,0,0,0,0]

temp=zip(names,classifiers,correction)
print(temp)

for name, clf,correct in temp:
    Xr=clf.fit_transform(X,Y)
    dddraw(Xr,name)
    res = sm.OLS(Y,Xr).fit()
    #print(res.summary())  # show OLS regression
    #print(res.predict(Xr).round()+correct)  #show OLS prediction
    #print('Ypredict',res.predict(Xr).round()+correct)  #show OLS prediction

    #print('Ypredict *log_sec',res.predict(Xr).round()+correct*Y.mean())  #show OLS prediction
    print(name,'%error',procenterror(res.predict(Xr)+correct*Y.mean(),Y),'rmsle',rmsle(res.predict(Xr)+correct*Y.mean(),Y))

# In[ ]:

from sklearn.linear_model import OrthogonalMatchingPursuit,RANSACRegressor,LogisticRegression,ElasticNetCV,HuberRegressor, Ridge, Lasso,LassoCV,Lars,BayesianRidge,SGDClassifier,LogisticRegressionCV,RidgeClassifier
from sklearn.preprocessing import MinMaxScaler

# import some data to play with
       # those ? converted to NAN are bothering me abit...

from sklearn.linear_model import OrthogonalMatchingPursuit,RANSACRegressor,LogisticRegression,ElasticNetCV,HuberRegressor, Ridge, Lasso,LassoCV,Lars,BayesianRidge,SGDClassifier,LogisticRegressionCV,RidgeClassifier
from sklearn.svm import SVC
from sklearn.preprocessing import MinMaxScaler,PolynomialFeatures
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier

param_grid = {'C': [0.1,1, 10, 100, 1000], 'gamma': [1,0.1,0.01,0.001,0.0001], 'kernel': ['rbf']}

X = df.drop('Radiation',axis=1) # we only take the first two features.
le = preprocessing.LabelEncoder()
def rmsle(y_predicted, y_real):
    return np.sqrt(np.mean(np.power(np.log1p(y_predicted)-np.log1p(y_real), 2)))
def procenterror(y_predicted, y_real):
     return np.round( np.mean(np.abs(y_predicted-y_real) )/ np.mean(y_real) *100 ,1)

Y=np.round(np.log(df['Radiation'])*10)
scaler = MinMaxScaler()
scaler.fit(X)
X=scaler.transform(X)
poly = PolynomialFeatures(2)
X=poly.fit_transform(X)

names = [    'KNN', 'DecisionTree', 'RandomForestClassifier',             ]

classifiers = [    KNeighborsClassifier(n_neighbors = 1), DecisionTreeClassifier(), RandomForestClassifier(n_estimators = 200),  HuberRegressor(fit_intercept=True, alpha=0.0, max_iter=100,epsilon=2.95), Ridge(fit_intercept=True, alpha=0.0, random_state=0, normalize=True), Lasso(alpha=0.05), LassoCV(), Lars(n_nonzero_coefs=10), BayesianRidge(), SGDClassifier(), RidgeClassifier(), LogisticRegression(), OrthogonalMatchingPursuit(),  ]
correction= [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

temp=zip(names,classifiers,correction)
print(temp)

for name, clf,correct in temp:
    regr=clf.fit(X,Y)
    #print( name,'% errors', abs(regr.predict(X)+correct-Y).sum()/(Y.sum())*100)
    print(name,'%error',procenterror(regr.predict(X),Y),'rmsle',rmsle(regr.predict(X),Y))
    from sklearn.metrics import classification_report, confusion_matrix, accuracy_score,f1_score, precision_score, recall_score

    # Confusion Matrix
    print(name,'Confusion Matrix')
    print(confusion_matrix(Y, np.round(regr.predict(X) ) ) )
    print('--'*40)

    # Classification Report
    print('Classification Report')
    print(classification_report(Y,np.round( regr.predict(X) ) ))

    # Accuracy
    print('--'*40)
    logreg_accuracy = round(accuracy_score(Y, np.round( regr.predict(X) ) ) * 100,2)
    print('Accuracy', logreg_accuracy,'%')
    df[name]=regr.predict(X)

# ### Model train

# In[ ]:

# Create the K-folds
k_folds = 5
kf = KFold(n_splits=k_folds, shuffle = True)

# Prepare dataset
X = df.drop(['Radiation','Data_UnixTimeStamp','TimeSunRise_UnixTimeStamp','TimeSunSet_UnixTimeStamp'] , axis=1).as_matrix()
y = df['Radiation'].as_matrix()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

# ### XGBoost

# In[ ]:

xgb_params = { 'n_trees': 50, 'eta': 0.05, 'max_depth': 5, 'subsample': 0.7, 'objective': 'reg:linear', 'eval_metric': 'rmse', 'silent': 1 }

dtrain = xgb.DMatrix(X_train, y_train)
dtest = xgb.DMatrix(X_test)

# In[ ]:

cv_output = xgb.cv(xgb_params, dtrain, num_boost_round=1000, early_stopping_rounds=50, verbose_eval=200, show_stdv=False)
cv_output[['train-rmse-mean', 'test-rmse-mean']].plot()
print()

# In[ ]:

num_boost_rounds = len(cv_output)
print(num_boost_rounds)
model = xgb.train(dict(xgb_params, silent=0), dtrain, num_boost_round= num_boost_rounds)

# In[ ]:

from sklearn.metrics import r2_score
print("R^2 in training: %s"  % r2_score(dtrain.get_label(), model.predict(dtrain)))
print("R^2 in testing: %s"  % r2_score(y_test, model.predict(dtest)))