<commit_before>import datetime
from sklearn import metrics
import pandas as pd
import numpy as np
import math
from statsmodels.discrete.discrete_model import Logit

def y_score(estimator, X):
    if hasattr(estimator, 'decision_function'):
        return estimator.decision_function(X)
    else:
        y = estimator.predict_proba(X)
        return y[:,1]

def sk_tree(X,y, params={'max_depth':3}):
    clf = tree.DecisionTreeClassifier(**params)
    return clf.fit(X, y)

def feature_importance(estimator, X):
    if hasattr(estimator, 'coef_'):
        i = estimator.coef_[0]
    elif hasattr(estimator, 'feature_importances_'):
        i = estimator.feature_importances_
    else:
        i = [np.nan]*len(X.columns)

    return pd.DataFrame({'feature': X.columns, 'importance': i}).sort('importance')

class LogisticRegression(object):
    def __init__(self):
        pass

    def fit(self, X, y, **kwargs):
        self.model = Logit(y, X)
        self.result = self.model.fit()

    def predict_proba(self, X):
        return self.result.predict(X)
<commit_msg>feature sort order<commit_after>import datetime
from sklearn import metrics
import pandas as pd
import numpy as np
import math
from statsmodels.discrete.discrete_model import Logit

def y_score(estimator, X):
    if hasattr(estimator, 'decision_function'):
        return estimator.decision_function(X)
    else:
        y = estimator.predict_proba(X)
        return y[:,1]

def sk_tree(X,y, params={'max_depth':3}):
    clf = tree.DecisionTreeClassifier(**params)
    return clf.fit(X, y)

def feature_importance(estimator, X):
    if hasattr(estimator, 'coef_'):
        i = estimator.coef_[0]
    elif hasattr(estimator, 'feature_importances_'):
        i = estimator.feature_importances_
    else:
        i = [np.nan]*len(X.columns)

    return pd.DataFrame({'feature': X.columns, 'importance': i}).sort('importance', ascending=False)

class LogisticRegression(object):
    def __init__(self):
        pass

    def fit(self, X, y, **kwargs):
        self.model = Logit(y, X)
        self.result = self.model.fit()

    def predict_proba(self, X):
        return self.result.predict(X)
<|endoftext|>


<commit_before>"""Build rules for XLA testing."""

load("@local_config_cuda//cuda:build_defs.bzl", "cuda_is_configured")
load("//tensorflow/compiler/xla/tests:plugin.bzl", "plugins")
load("//tensorflow:tensorflow.bzl", "tf_cc_test")
load(
    "//tensorflow/core:platform/default/build_config_root.bzl",
    "tf_cuda_tests_tags",
)

all_backends = ["cpu", "gpu"] + plugins.keys()

def filter_backends(backends):
    """Removes "gpu" from a backend list if CUDA is not enabled.

    This allows us to simply hardcode lists including "gpu" here and in the
    BUILD file, without causing failures when CUDA isn't enabled.'

    Args:
      backends: A list of backends to filter.

    Returns:
      The filtered list of backends.
    """
    if cuda_is_configured():
        return backends
    else:
        return [backend for backend in backends if backend != "gpu"]

def xla_test(
        name,
        srcs,
        deps,
        xla_test_library_deps = [],
        backends = [],
        blacklisted_backends = [],
        args = [],
        tags = [],
        copts = [],
        data = [],
        backend_tags = {},
        backend_args = {},
        **kwargs):
    """Generates cc_test targets for the given XLA backends.

    This rule generates a cc_test target for one or more XLA backends and also a
    platform-agnostic cc_library rule. The arguments are identical to cc_test with
    two additions: 'backends' and 'backend_args'. 'backends' specifies the
    backends to generate tests for ("cpu", "gpu"), and
    'backend_args'/'backend_tags' specifies backend-specific args parameters to
    use when generating the cc_test.

    The name of the cc_tests are the provided name argument with the backend name
    appended, and the cc_library target name is the provided name argument with
    "_lib" appended. For example, if name parameter is "foo_test", then the cpu
    test target will be "foo_test_cpu" and the cc_library target is "foo_lib".

    The cc_library target can be used to link with other plugins outside of
    xla_test.

    The build rule also defines a test suite ${name} which includes the tests for
    each of the supported backends.

    Each generated cc_test target has a tag indicating which backend the test is
    for. This tag is of the form "xla_${BACKEND}" (eg, "xla_cpu"). These
    tags can be used to gather tests for a particular backend into a test_suite.

    Examples:

      # Generates the targets: foo_test_cpu and foo_test_gpu.
      xla_test(
          name = "foo_test",
          srcs = ["foo_test.cc"],
          backends = ["cpu", "gpu"],
          deps = [...],
      )

      # Generates the targets: bar_test_cpu and bar_test_gpu. bar_test_cpu
      # includes the additional arg "--special_cpu_flag".
      xla_test(
          name = "bar_test",
          srcs = ["bar_test.cc"],
          backends = ["cpu", "gpu"],
          backend_args = {"cpu": ["--special_cpu_flag"]}
          deps = [...],
      )

    The build rule defines the preprocessor macro XLA_TEST_BACKEND_${BACKEND}
    to the value 1 where ${BACKEND} is the uppercase name of the backend.

    Args:
      name: Name of the target.
      srcs: Sources for the target.
      deps: Dependencies of the target.
      xla_test_library_deps: If set, the generated test targets will depend on the
        respective cc_libraries generated by the xla_test_library rule.
      backends: A list of backends to generate tests for. Supported values: "cpu",
        "gpu". If this list is empty, the test will be generated for all supported
        backends.
      blacklisted_backends: A list of backends to NOT generate tests for.
      args: Test arguments for the target.
      tags: Tags for the target.
      copts: Additional copts to pass to the build.
      data: Additional data to pass to the build.
      backend_tags: A dict mapping backend name to list of additional tags to
        use for that target.
      backend_args: A dict mapping backend name to list of additional args to
        use for that target.
      **kwargs: Additional keyword arguments to pass to native.cc_test.
    """
    test_names = []
    if not backends:
        backends = all_backends

    backends = [
        backend
        for backend in backends
        if backend not in blacklisted_backends
    ]

    native.cc_library(
        name = "%s_lib" % name,
        srcs = srcs,
        copts = copts,
        testonly = True,
        deps = deps + ["//tensorflow/compiler/xla/tests:test_macros_header"],
    )

    for backend in filter_backends(backends):
        test_name = "%s_%s" % (name, backend)
        this_backend_tags = ["xla_%s" % backend]
        this_backend_copts = []
        this_backend_args = backend_args.get(backend, [])
        this_backend_data = []
        if backend == "cpu":
            backend_deps = ["//tensorflow/compiler/xla/service:cpu_plugin"]
            backend_deps += ["//tensorflow/compiler/xla/tests:test_macros_cpu"]
        elif backend == "gpu":
            backend_deps = ["//tensorflow/compiler/xla/service:gpu_plugin"]
            backend_deps += ["//tensorflow/compiler/xla/tests:test_macros_gpu"]
            this_backend_tags += tf_cuda_tests_tags()
        elif backend in plugins:
            backend_deps = []
            backend_deps += plugins[backend]["deps"]
            this_backend_copts += plugins[backend]["copts"]
            this_backend_tags += plugins[backend]["tags"]
            this_backend_args += plugins[backend]["args"]
            this_backend_data += plugins[backend]["data"]
        else:
            fail("Unknown backend %s" % backend)

        if xla_test_library_deps:
            for lib_dep in xla_test_library_deps:
                backend_deps += ["%s_%s" % (lib_dep, backend)]

        tf_cc_test(
            name = test_name,
            srcs = srcs,
            tags = tags + backend_tags.get(backend, []) + this_backend_tags,
            extra_copts = copts + ["-DXLA_TEST_BACKEND_%s=1" % backend.upper()] +
                          this_backend_copts,
            args = args + this_backend_args,
            deps = deps + backend_deps,
            data = data + this_backend_data,
            **kwargs
        )

        test_names.append(test_name)

    native.test_suite(name = name, tests = test_names)

def xla_test_library(
        name,
        srcs,
        hdrs = [],
        deps = [],
        backends = []):
    """Generates cc_library targets for the given XLA backends.

    This rule forces the sources to be compiled for each backend so that the
    backend specific macros could expand correctly. It's useful when test targets
    in different directories referring to the same sources but test with different
    arguments.

    Examples:

      # Generates the targets: foo_test_library_cpu and foo_test_gpu.
      xla_test_library(
          name = "foo_test_library",
          srcs = ["foo_test.cc"],
          backends = ["cpu", "gpu"],
          deps = [...],
      )
      # Then use the xla_test rule to generate test targets:
      xla_test(
          name = "foo_test",
          srcs = [],
          backends = ["cpu", "gpu"],
          deps = [...],
          xla_test_library_deps = [":foo_test_library"],
      )

    Args:
      name: Name of the target.
      srcs: Sources for the target.
      hdrs: Headers for the target.
      deps: Dependencies of the target.
      backends: A list of backends to generate libraries for.
        Supported values: "cpu", "gpu". If this list is empty, the
        library will be generated for all supported backends.
    """

    if not backends:
        backends = all_backends

    for backend in filter_backends(backends):
        this_backend_copts = []
        if backend in ["cpu", "gpu"]:
            backend_deps = ["//tensorflow/compiler/xla/tests:test_macros_%s" % backend]
        elif backend in plugins:
            backend_deps = plugins[backend]["deps"]
            this_backend_copts += plugins[backend]["copts"]
        else:
            fail("Unknown backend %s" % backend)

        native.cc_library(
            name = "%s_%s" % (name, backend),
            srcs = srcs,
            testonly = True,
            hdrs = hdrs,
            copts = ["-DXLA_TEST_BACKEND_%s=1" % backend.upper()] +
                    this_backend_copts,
            deps = deps + backend_deps,
        )

def generate_backend_suites(backends = []):
    if not backends:
        backends = all_backends
    for backend in filter_backends(backends):
        native.test_suite(
            name = "%s_tests" % backend,
            tags = ["xla_%s" % backend],
        )

def generate_backend_test_macros(backends = []):
    if not backends:
        backends = all_backends
    for backend in filter_backends(backends):
        manifest = ""
        if backend in plugins:
            manifest = plugins[backend]["disabled_manifest"]

        native.cc_library(
            name = "test_macros_%s" % backend,
            testonly = True,
            srcs = ["test_macros.cc"],
            hdrs = ["test_macros.h"],
            copts = [
                "-DXLA_PLATFORM=\\\"%s\\\"" % backend.upper(),
                "-DXLA_DISABLED_MANIFEST=\\\"%s\\\"" % manifest,
            ],
            deps = [
                "//tensorflow/compiler/xla:types",
                "//tensorflow/core:lib",
                "//tensorflow/core:regexp_internal",
                "//tensorflow/core:test",
            ],
        )
<commit_msg>Make sure broken tests are filtered out in XLA tests suites.<commit_after>"""Build rules for XLA testing."""

load("@local_config_cuda//cuda:build_defs.bzl", "cuda_is_configured")
load("//tensorflow/compiler/xla/tests:plugin.bzl", "plugins")
load("//tensorflow:tensorflow.bzl", "tf_cc_test")
load(
    "//tensorflow/core:platform/default/build_config_root.bzl",
    "tf_cuda_tests_tags",
)

all_backends = ["cpu", "gpu"] + plugins.keys()

def filter_backends(backends):
    """Removes "gpu" from a backend list if CUDA is not enabled.

    This allows us to simply hardcode lists including "gpu" here and in the
    BUILD file, without causing failures when CUDA isn't enabled.'

    Args:
      backends: A list of backends to filter.

    Returns:
      The filtered list of backends.
    """
    if cuda_is_configured():
        return backends
    else:
        return [backend for backend in backends if backend != "gpu"]

def xla_test(
        name,
        srcs,
        deps,
        xla_test_library_deps = [],
        backends = [],
        blacklisted_backends = [],
        args = [],
        tags = [],
        copts = [],
        data = [],
        backend_tags = {},
        backend_args = {},
        **kwargs):
    """Generates cc_test targets for the given XLA backends.

    This rule generates a cc_test target for one or more XLA backends and also a
    platform-agnostic cc_library rule. The arguments are identical to cc_test with
    two additions: 'backends' and 'backend_args'. 'backends' specifies the
    backends to generate tests for ("cpu", "gpu"), and
    'backend_args'/'backend_tags' specifies backend-specific args parameters to
    use when generating the cc_test.

    The name of the cc_tests are the provided name argument with the backend name
    appended, and the cc_library target name is the provided name argument with
    "_lib" appended. For example, if name parameter is "foo_test", then the cpu
    test target will be "foo_test_cpu" and the cc_library target is "foo_lib".

    The cc_library target can be used to link with other plugins outside of
    xla_test.

    The build rule also defines a test suite ${name} which includes the tests for
    each of the supported backends.

    Each generated cc_test target has a tag indicating which backend the test is
    for. This tag is of the form "xla_${BACKEND}" (eg, "xla_cpu"). These
    tags can be used to gather tests for a particular backend into a test_suite.

    Examples:

      # Generates the targets: foo_test_cpu and foo_test_gpu.
      xla_test(
          name = "foo_test",
          srcs = ["foo_test.cc"],
          backends = ["cpu", "gpu"],
          deps = [...],
      )

      # Generates the targets: bar_test_cpu and bar_test_gpu. bar_test_cpu
      # includes the additional arg "--special_cpu_flag".
      xla_test(
          name = "bar_test",
          srcs = ["bar_test.cc"],
          backends = ["cpu", "gpu"],
          backend_args = {"cpu": ["--special_cpu_flag"]}
          deps = [...],
      )

    The build rule defines the preprocessor macro XLA_TEST_BACKEND_${BACKEND}
    to the value 1 where ${BACKEND} is the uppercase name of the backend.

    Args:
      name: Name of the target.
      srcs: Sources for the target.
      deps: Dependencies of the target.
      xla_test_library_deps: If set, the generated test targets will depend on the
        respective cc_libraries generated by the xla_test_library rule.
      backends: A list of backends to generate tests for. Supported values: "cpu",
        "gpu". If this list is empty, the test will be generated for all supported
        backends.
      blacklisted_backends: A list of backends to NOT generate tests for.
      args: Test arguments for the target.
      tags: Tags for the target.
      copts: Additional copts to pass to the build.
      data: Additional data to pass to the build.
      backend_tags: A dict mapping backend name to list of additional tags to
        use for that target.
      backend_args: A dict mapping backend name to list of additional args to
        use for that target.
      **kwargs: Additional keyword arguments to pass to native.cc_test.
    """
    test_names = []
    if not backends:
        backends = all_backends

    backends = [
        backend
        for backend in backends
        if backend not in blacklisted_backends
    ]

    native.cc_library(
        name = "%s_lib" % name,
        srcs = srcs,
        copts = copts,
        testonly = True,
        deps = deps + ["//tensorflow/compiler/xla/tests:test_macros_header"],
    )

    for backend in filter_backends(backends):
        test_name = "%s_%s" % (name, backend)
        this_backend_tags = ["xla_%s" % backend]
        this_backend_copts = []
        this_backend_args = backend_args.get(backend, [])
        this_backend_data = []
        if backend == "cpu":
            backend_deps = ["//tensorflow/compiler/xla/service:cpu_plugin"]
            backend_deps += ["//tensorflow/compiler/xla/tests:test_macros_cpu"]
        elif backend == "gpu":
            backend_deps = ["//tensorflow/compiler/xla/service:gpu_plugin"]
            backend_deps += ["//tensorflow/compiler/xla/tests:test_macros_gpu"]
            this_backend_tags += tf_cuda_tests_tags()
        elif backend in plugins:
            backend_deps = []
            backend_deps += plugins[backend]["deps"]
            this_backend_copts += plugins[backend]["copts"]
            this_backend_tags += plugins[backend]["tags"]
            this_backend_args += plugins[backend]["args"]
            this_backend_data += plugins[backend]["data"]
        else:
            fail("Unknown backend %s" % backend)

        if xla_test_library_deps:
            for lib_dep in xla_test_library_deps:
                backend_deps += ["%s_%s" % (lib_dep, backend)]

        tf_cc_test(
            name = test_name,
            srcs = srcs,
            tags = tags + backend_tags.get(backend, []) + this_backend_tags,
            extra_copts = copts + ["-DXLA_TEST_BACKEND_%s=1" % backend.upper()] +
                          this_backend_copts,
            args = args + this_backend_args,
            deps = deps + backend_deps,
            data = data + this_backend_data,
            **kwargs
        )

        test_names.append(test_name)

    native.test_suite(name = name, tests = test_names)

def xla_test_library(
        name,
        srcs,
        hdrs = [],
        deps = [],
        backends = []):
    """Generates cc_library targets for the given XLA backends.

    This rule forces the sources to be compiled for each backend so that the
    backend specific macros could expand correctly. It's useful when test targets
    in different directories referring to the same sources but test with different
    arguments.

    Examples:

      # Generates the targets: foo_test_library_cpu and foo_test_gpu.
      xla_test_library(
          name = "foo_test_library",
          srcs = ["foo_test.cc"],
          backends = ["cpu", "gpu"],
          deps = [...],
      )
      # Then use the xla_test rule to generate test targets:
      xla_test(
          name = "foo_test",
          srcs = [],
          backends = ["cpu", "gpu"],
          deps = [...],
          xla_test_library_deps = [":foo_test_library"],
      )

    Args:
      name: Name of the target.
      srcs: Sources for the target.
      hdrs: Headers for the target.
      deps: Dependencies of the target.
      backends: A list of backends to generate libraries for.
        Supported values: "cpu", "gpu". If this list is empty, the
        library will be generated for all supported backends.
    """

    if not backends:
        backends = all_backends

    for backend in filter_backends(backends):
        this_backend_copts = []
        if backend in ["cpu", "gpu"]:
            backend_deps = ["//tensorflow/compiler/xla/tests:test_macros_%s" % backend]
        elif backend in plugins:
            backend_deps = plugins[backend]["deps"]
            this_backend_copts += plugins[backend]["copts"]
        else:
            fail("Unknown backend %s" % backend)

        native.cc_library(
            name = "%s_%s" % (name, backend),
            srcs = srcs,
            testonly = True,
            hdrs = hdrs,
            copts = ["-DXLA_TEST_BACKEND_%s=1" % backend.upper()] +
                    this_backend_copts,
            deps = deps + backend_deps,
        )

def generate_backend_suites(backends = []):
    if not backends:
        backends = all_backends
    for backend in filter_backends(backends):
        native.test_suite(
            name = "%s_tests" % backend,
            tags = ["xla_%s" % backend, "-broken", "manual"],
        )

def generate_backend_test_macros(backends = []):
    if not backends:
        backends = all_backends
    for backend in filter_backends(backends):
        manifest = ""
        if backend in plugins:
            manifest = plugins[backend]["disabled_manifest"]

        native.cc_library(
            name = "test_macros_%s" % backend,
            testonly = True,
            srcs = ["test_macros.cc"],
            hdrs = ["test_macros.h"],
            copts = [
                "-DXLA_PLATFORM=\\\"%s\\\"" % backend.upper(),
                "-DXLA_DISABLED_MANIFEST=\\\"%s\\\"" % manifest,
            ],
            deps = [
                "//tensorflow/compiler/xla:types",
                "//tensorflow/core:lib",
                "//tensorflow/core:regexp_internal",
                "//tensorflow/core:test",
            ],
        )
<|endoftext|>


<commit_before>#!/usr/bin/python

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

#
# (lldb) command script import llvmDebugInfoC/src/scripts/konan_lldb.py
# (lldb) p kotlin_variable
#

import lldb
import struct

NULL = 'null'

def lldb_val_to_ptr(lldb_val):
    addr = lldb_val.GetValueAsUnsigned()
    return '((struct ObjHeader *) {:#x})'.format(addr)

def evaluate(expr):
    return lldb.debugger.GetSelectedTarget().EvaluateExpression(expr, lldb.SBExpressionOptions())

def is_instance_of(addr, typeinfo):
    return evaluate("(bool)IsInstance({}, {})".format(addr, typeinfo)).GetValue() == "true"

def is_string(value):
    return is_instance_of(lldb_val_to_ptr(value), "theStringTypeInfo")

def is_array(value):
    return int(evaluate("(int)Konan_DebugIsArray({})".format(lldb_val_to_ptr(value))).GetValue()) == 1

def check_type_info(addr):
    """This method checks self-referencing of pointer of first member of TypeInfo including case when object has an
    meta-object pointed by TypeInfo. """
    result = evaluate("**(void ***){0} == ***(void****){0}".format(addr))
    return result.IsValid() and result.GetValue() == "true"

#
# Some kind of forward declaration.

__FACTORY = {}

def kotlin_object_type_summary(lldb_val, internal_dict):
    """Hook that is run by lldb to display a Kotlin object."""
    fallback = lldb_val.GetValue()
    if str(lldb_val.type) != "struct ObjHeader *":
        return fallback

    if not check_type_info(fallback):
        return NULL

    ptr = lldb_val_to_ptr(lldb_val)
    if ptr is None:
        return fallback

    return select_provider(lldb_val).to_string()

def select_provider(lldb_val):
    return __FACTORY['string'](lldb_val) if is_string(lldb_val) else __FACTORY['array'](lldb_val) if is_array(
    lldb_val) else __FACTORY['object'](lldb_val)

class KonanHelperProvider(lldb.SBSyntheticValueProvider):
    def __init__(self, valobj):
        self._target = lldb.debugger.GetSelectedTarget()
        self._process = self._target.GetProcess()
        self._valobj = valobj
        self._ptr = lldb_val_to_ptr(self._valobj)
        if is_string(valobj):
            return
        self._children_count = 0
        self._children = []
        self._type_conversion = {0: lambda address, _: "<invalid>{:#x}".format(address),
                                 1: lambda address, _: kotlin_object_type_summary(evaluate("(*(struct ObjHeader **){:#x})".format(address)), {}),
                                 2: lambda address, error: self.__read_memory(address, "<c", 1, error),
                                 3: lambda address, error: self.__read_memory(address, "<h", 2, error),
                                 4: lambda address, error: self.__read_memory(address, "<i", 4, error),
                                 5: lambda address, error: self.__read_memory(address, "<q", 8, error),
                                 6: lambda address, error: self.__read_memory(address, "<f", 4, error),
                                 7: lambda address, error: self.__read_memory(address, "<d", 8, error),
                                 8: lambda address, _: "(void *){:#x}".format(address),
                                 9: lambda address, error: self.__read_memory(address, "<?", 4, error)}

    def update(self):
        self._children_count = int(evaluate("(int)Konan_DebugGetFieldCount({})".format(self._ptr)).GetValue())

    def _read_string(self, expr, error):
        return self._process.ReadCStringFromMemory(int(evaluate(expr).GetValue(), 0), 0x1000, error)

    def _read_value(self, index, error):
        value_type = evaluate("(int)Konan_DebugGetFieldType({}, {})".format(self._ptr, index)).GetValue()
        address = int(evaluate("(void *)Konan_DebugGetFieldAddress({}, {})".format(self._ptr, index)).GetValue(), 0)
        return self._type_conversion[int(value_type)](address, error)

    def __read_memory(self, address, fmt, size, error):
        content = self._process.ReadMemory(address, size, error)
        return struct.unpack(fmt, content)[0] if error.Success() else "error: {:#x}".format(address)

class KonanStringSyntheticProvider(KonanHelperProvider):
    def __init__(self, valobj):
        super(KonanStringSyntheticProvider, self).__init__(valobj)
        fallback = valobj.GetValue()
        buff_len = evaluate(
            '(int)Konan_DebugObjectToUtf8Array({}, (char *)Konan_DebugBuffer(), (int)Konan_DebugBufferSize());'.format(self._ptr)
        ).unsigned

        if not buff_len:
            self._representation = fallback
            return

        buff_addr = evaluate("(char *)Konan_DebugBuffer()").unsigned

        error = lldb.SBError()
        s = self._process.ReadCStringFromMemory(int(buff_addr), int(buff_len), error)
        self._representation = s if error.Success() else fallback

    def update(self):
        pass

    def num_children(self):
        return 0

    def has_children(self):
        return False

    def get_child_index(self, _):
        return None

    def get_child_at_index(self, _):
        return None

    def to_string(self):
        return self._representation

class KonanObjectSyntheticProvider(KonanHelperProvider):
    def __init__(self, valobj):
        super(KonanObjectSyntheticProvider, self).__init__(valobj)
        self.update()
        pass

    def update(self):
        super(KonanObjectSyntheticProvider, self).update()
        error = lldb.SBError()
        self._children = [
            self._read_string("(const char *)Konan_DebugGetFieldName({}, (int){})".format(self._ptr, i), error) for i in
            range(0, self._children_count) if error.Success()]

    def num_children(self):
        return self._children_count

    def has_children(self):
        return self._children_count > 0

    def get_child_index(self, name):
        return self._children.index(name)

    def get_child_at_index(self, index):
        return evaluate("(void *)Konan_DebugGetFieldAddress({}, {})".format(self._ptr, index)).GetValue()

    def to_string(self):
        error = lldb.SBError()
        return dict([(self._children[i], self._read_value(i, error)) for i in range(0, self._children_count)])

class KonanArraySyntheticProvider(KonanHelperProvider):
    def __init__(self, valobj):
        super(KonanArraySyntheticProvider, self).__init__(valobj)
        if self._ptr is None:
            return
        self.update()

    def update(self):
        super(KonanArraySyntheticProvider, self).update()
        self._children = [x for x in range(0, self.num_children())]

    def num_children(self):
        return self._children_count

    def has_children(self):
        return self._children_count > 0

    def get_child_index(self, name):
        index = int(name)
        return index if (0 <= index < self._num_children) else None

    def get_child_at_index(self, index):
        error = lldb.SBError()
        return self._read_value(index, error) if error.Success() else None

    def to_string(self):
        error = lldb.SBError()
        return [self._read_value(i, error) for i in range(0, self._children_count)]

class KonanProxyTypeProvider:
    def __init__(self, valobj, _):
        fallback = int(valobj.GetValue(), 0)
        if not check_type_info(fallback):
            return
        self._proxy = select_provider(valobj)
        self.update()

    def __getattr__(self, item):
        return getattr(self._proxy, item)

def __lldb_init_module(debugger, _):
    __FACTORY['object'] = lambda x: KonanObjectSyntheticProvider(x)
    __FACTORY['array'] = lambda x: KonanArraySyntheticProvider(x)
    __FACTORY['string'] = lambda x: KonanStringSyntheticProvider(x)
    debugger.HandleCommand('\
        type summary add \
        --no-value \
        --expand \
        --python-function konan_lldb.kotlin_object_type_summary \
        "ObjHeader *" \
        --category Kotlin\
    ')
    debugger.HandleCommand('\
        type synthetic add \
        --python-class konan_lldb.KonanProxyTypeProvider\
        "ObjHeader *" \
        --category Kotlin\
    ')
    debugger.HandleCommand('type category enable Kotlin')
<commit_msg>Improve synthetic field accessors.<commit_after>#!/usr/bin/python

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

#
# (lldb) command script import llvmDebugInfoC/src/scripts/konan_lldb.py
# (lldb) p kotlin_variable
#

import lldb
import struct

NULL = 'null'

def lldb_val_to_ptr(lldb_val):
    addr = lldb_val.GetValueAsUnsigned()
    return '((struct ObjHeader *) {:#x})'.format(addr)

def evaluate(expr):
    return lldb.debugger.GetSelectedTarget().EvaluateExpression(expr, lldb.SBExpressionOptions())

def is_instance_of(addr, typeinfo):
    return evaluate("(bool)IsInstance({}, {})".format(addr, typeinfo)).GetValue() == "true"

def is_string(value):
    return is_instance_of(lldb_val_to_ptr(value), "theStringTypeInfo")

def is_array(value):
    return int(evaluate("(int)Konan_DebugIsArray({})".format(lldb_val_to_ptr(value))).GetValue()) == 1

def check_type_info(addr):
    """This method checks self-referencing of pointer of first member of TypeInfo including case when object has an
    meta-object pointed by TypeInfo. """
    result = evaluate("**(void ***){0} == ***(void****){0}".format(addr))
    return result.IsValid() and result.GetValue() == "true"

#
# Some kind of forward declaration.

__FACTORY = {}

def kotlin_object_type_summary(lldb_val, internal_dict):
    """Hook that is run by lldb to display a Kotlin object."""
    fallback = lldb_val.GetValue()
    if str(lldb_val.type) != "struct ObjHeader *":
        return fallback

    if not check_type_info(fallback):
        return NULL

    ptr = lldb_val_to_ptr(lldb_val)
    if ptr is None:
        return fallback

    return select_provider(lldb_val).to_string()

def select_provider(lldb_val):
    return __FACTORY['string'](lldb_val) if is_string(lldb_val) else __FACTORY['array'](lldb_val) if is_array(
    lldb_val) else __FACTORY['object'](lldb_val)

class KonanHelperProvider(lldb.SBSyntheticValueProvider):
    def __init__(self, valobj):
        self._target = lldb.debugger.GetSelectedTarget()
        self._process = self._target.GetProcess()
        self._valobj = valobj
        self._ptr = lldb_val_to_ptr(self._valobj)
        if is_string(valobj):
            return
        self._children_count = 0
        self._children = []
        self._type_conversion = {0: lambda address, _: "<invalid>{:#x}".format(address),
                                 1: lambda address, _: kotlin_object_type_summary(evaluate("(*(struct ObjHeader **){:#x})".format(address)), {}),
                                 2: lambda address, error: self.__read_memory(address, "<c", 1, error),
                                 3: lambda address, error: self.__read_memory(address, "<h", 2, error),
                                 4: lambda address, error: self.__read_memory(address, "<i", 4, error),
                                 5: lambda address, error: self.__read_memory(address, "<q", 8, error),
                                 6: lambda address, error: self.__read_memory(address, "<f", 4, error),
                                 7: lambda address, error: self.__read_memory(address, "<d", 8, error),
                                 8: lambda address, _: "(void *){:#x}".format(address),
                                 # TODO: or 1?
                                 9: lambda address, error: self.__read_memory(address, "<?", 4, error)}

    def update(self):
        self._children_count = int(evaluate("(int)Konan_DebugGetFieldCount({})".format(self._ptr)).GetValue())

    def _read_string(self, expr, error):
        return self._process.ReadCStringFromMemory(long(evaluate(expr).GetValue(), 0), 0x1000, error)

    def _read_value(self, index, error):
        value_type = evaluate("(int)Konan_DebugGetFieldType({}, {})".format(self._ptr, index)).GetValue()
        address = long(evaluate("(void *)Konan_DebugGetFieldAddress({}, {})".format(self._ptr, index)).GetValue(), 0)
        return self._type_conversion[int(value_type)](address, error)

    def __read_memory(self, address, fmt, size, error):
        content = self._process.ReadMemory(address, size, error)
        return struct.unpack(fmt, content)[0] if error.Success() else "error: {:#x}".format(address)

class KonanStringSyntheticProvider(KonanHelperProvider):
    def __init__(self, valobj):
        super(KonanStringSyntheticProvider, self).__init__(valobj)
        fallback = valobj.GetValue()
        buff_len = evaluate(
            '(int)Konan_DebugObjectToUtf8Array({}, (char *)Konan_DebugBuffer(), (int)Konan_DebugBufferSize());'.format(self._ptr)
        ).unsigned

        if not buff_len:
            self._representation = fallback
            return

        buff_addr = evaluate("(char *)Konan_DebugBuffer()").unsigned

        error = lldb.SBError()
        s = self._process.ReadCStringFromMemory(long(buff_addr), int(buff_len), error)
        self._representation = s if error.Success() else fallback

    def update(self):
        pass

    def num_children(self):
        return 0

    def has_children(self):
        return False

    def get_child_index(self, _):
        return None

    def get_child_at_index(self, _):
        return None

    def to_string(self):
        return self._representation

class KonanObjectSyntheticProvider(KonanHelperProvider):
    def __init__(self, valobj):
        super(KonanObjectSyntheticProvider, self).__init__(valobj)
        self.update()

    def update(self):
        super(KonanObjectSyntheticProvider, self).update()
        error = lldb.SBError()
        self._children = [
            self._read_string("(const char *)Konan_DebugGetFieldName({}, (int){})".format(self._ptr, i), error) for i in
            range(0, self._children_count) if error.Success()]
        return True

    def num_children(self):
        return self._children_count

    def has_children(self):
        return self._children_count > 0

    def get_child_index(self, name):
        if not name in self._children:
            return -1
        return self._children.index(name)

    def get_child_at_index(self, index):
        if index < 0 or index >= self._children_count:
            return None
        error = lldb.SBError()
        value = self._read_value(index, error)
        return value if error.Success() else None

    # TODO: fix cyclic structures stringification.
    def to_string(self):
        error = lldb.SBError()
        return dict([(self._children[i], self._read_value(i, error)) for i in range(0, self._children_count)])

class KonanArraySyntheticProvider(KonanHelperProvider):
    def __init__(self, valobj):
        super(KonanArraySyntheticProvider, self).__init__(valobj)
        if self._ptr is None:
            return
        self.update()

    def update(self):
        super(KonanArraySyntheticProvider, self).update()
        self._children = [x for x in range(0, self.num_children())]
        return True

    def num_children(self):
        return self._children_count

    def has_children(self):
        return self._children_count > 0

    def get_child_index(self, name):
        index = int(name)
        return index if (0 <= index < self._children_count) else -1

    def get_child_at_index(self, index):
        if index < 0 or index >= self._children_count:
            return None
        error = lldb.SBError()
        return self._read_value(index, error) if error.Success() else None

    def to_string(self):
        error = lldb.SBError()
        return [self._read_value(i, error) for i in range(0, self._children_count)]

class KonanProxyTypeProvider:
    def __init__(self, valobj, _):
        fallback = int(valobj.GetValue(), 0)
        if not check_type_info(fallback):
            return
        self._proxy = select_provider(valobj)
        self.update()

    def __getattr__(self, item):
        return getattr(self._proxy, item)

def __lldb_init_module(debugger, _):
    __FACTORY['object'] = lambda x: KonanObjectSyntheticProvider(x)
    __FACTORY['array'] = lambda x: KonanArraySyntheticProvider(x)
    __FACTORY['string'] = lambda x: KonanStringSyntheticProvider(x)
    debugger.HandleCommand('\
        type summary add \
        --no-value \
        --expand \
        --python-function konan_lldb.kotlin_object_type_summary \
        "ObjHeader *" \
        --category Kotlin\
    ')
    debugger.HandleCommand('\
        type synthetic add \
        --python-class konan_lldb.KonanProxyTypeProvider\
        "ObjHeader *" \
        --category Kotlin\
    ')
    debugger.HandleCommand('type category enable Kotlin')
<|endoftext|>


<commit_before>import wikipedia
from textblob import TextBlob

class Article:
    """Retrieves and analyzes wikipedia articles"""

    def __init__(self, title):
        self.title = title
        self.page = wikipedia.page(title)
        self.summary = TextBlob(self.page.summary)
        self.body = TextBlob(self.page.content)

    def is_unusual_word(self, word):
        # Maybe measure against summary instead?
        return self.body.words.count(word) < 5

    def generate_trivia_sentences(self):
        trivia_sentences = []
        for sentence in self.body.sentences:
            trivia = self.evaluate_sentence(sentence)
            if trivia:
                trivia_sentences.append(str(trivia))

        return trivia_sentences

    def evaluate_sentence(self, sentence):
        tag_map = {word: tag for word, tag in sentence.tags}

        replace_nouns = []
        for word, tag in sentence.tags:
            if tag == 'NN':
                # Is it unusual compared to other words in this article?
                # If not, it probably won't make for good trivia
                if not self.is_unusual_word(word):
                    break

                # Is it in a noun phrase? If so, blank out everything in that phrase
                for phrase in sentence.noun_phrases:
                    if word in phrase:
                        [replace_nouns.append(word) for word in phrase.split() if tag_map[word] == 'NN']
                        break
                    else:
                        replace_nouns.append(word)
                break

        if len(replace_nouns) == 0:
            # No words or phrases are unusual enough in this sentence
            # to make good trivia
            return None

        for word in replace_nouns:
            sentence = sentence.replace(word, '__________')
        return sentence
<commit_msg>Going back to TextBlob<commit_after>import wikipedia
from textblob import TextBlob

class Article:
    """Retrieves and analyzes wikipedia articles"""

    def __init__(self, title):
        self.title = title
        self.page = wikipedia.page(title)
        self.summary = TextBlob(self.page.summary)
        self.body = TextBlob(self.page.content)

    def is_unusual_word(self, word):
        return self.body.words.count(word) < 5

    def generate_trivia_sentences(self):
        trivia_sentences = []
        for sentence in self.summary.sentences:
            trivia = self.evaluate_sentence(sentence)
            if trivia:
                trivia_sentences.append(str(trivia))

        return trivia_sentences

    def evaluate_sentence(self, sentence):
        replace_nouns = []
        for counter, tag in enumerate(sentence.tags):
            if tag[1] == 'NN':
                noun = tag[0]

                # Is it unusual compared to other words in this article?
                # If not, it probably won't make for good trivia
                if not self.is_unusual_word(noun):
                    break

                # Is it in a noun phrase? If so, blank out everything in that phrase
                for phrase in sentence.noun_phrases:
                    if noun in phrase:
                        [replace_nouns.append(word) for word in phrase.split()]
                        break
                    else:
                        replace_nouns.append(noun)
                break

        if len(replace_nouns) == 0:
            # No words or phrases are unusual enough in this sentence
            # to make good trivia
            return None

        for word in replace_nouns:
            sentence = sentence.replace(word, '__________')
        return sentence
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from nose.plugins.base import Plugin
from nose.case import Test
import logging
import unittest
import os

log = logging.getLogger(__name__)

class Goober(Plugin):
    """
    After a multiprocess test run, print out a one-line command that will rerun all the failed / error'd tests
    """
    name = "goober"
    score = 2
    enableOpt = "goober"
    activate = "--goober"

    def __init__(self):
        super(Goober, self).__init__()

    def options(self, parser, env):
        parser.add_option('--goober',
                          action='store_true',
                          help="print failed test paths: %s" %
                          (self.help()))
        parser.add_option('--goober-prefix',
                          action='store',
                          type='string',
                          dest='prefix',
                          help="Environment variables to prepend to goober's output. For example, --goober-prefix='LOCALE' will attach 'LOCALE=<os.environ.get('LOCALE')> to 'nosetests -v --goober'")
        parser.add_option('--goober-extra',
                          action='store',
                          help="append additional nose options to goober's output (give comma separated). For example: --goober-extra=--with-xunit,--with-xcover,--xcoverage-file=coverage.xml")

        super(Goober, self).options(parser, env)

    def configure(self, options, conf):
        super(Goober, self).configure(options, conf)
        self.prefix = ''
        if not options.prefix:
            return
        self.env_vars = options.prefix.split(',')
        if self.env_vars:
            self.assemble_prefix()
        if options.goober_extra:
            self.extra_options = str(options.goober_extra).replace(","," ")

    def assemble_prefix(self):
        self.prefix += ' '.join(["%s=%s" % (var, os.environ.get(var)) for var in self.env_vars if os.environ.get(var) is not None]) + ' '

    def get_output(self, test):
        try:
            info = test.test
        except AttributeError:
            info = test._id
        bits = str(info).split('.')
        testname = bits.pop()
        path = "/".join(bits)
        path += ".py:%s" % testname
        return path

    def determine_test_path(self, problem):
        """
        When we receive a multiprocess failure/error object, there is a stacktrace and an id. The id is a string representation of the path to the test that failed. But, no determination is made about whether the test that failed was part of a test class, or a standalone test.

        We will break that id into pieces, splitting on '.' - we will re-assemble that id as a path, from the begnning, and search for it in the stacktrace. The first time we don't find it marks the end of the file path, and the beginning of the name of the test class/specific test. We keep the dots separating that part.
        """
        test, trace = problem
        try:
            test_id = test.id()
        except AttributeError:
            test_id = test._id

        test_bits = str(test_id).split('.')
        path = ''
        while path in trace:
            bit = test_bits.pop(0)
            path += bit + "/"
        path = path.rstrip('/') + '.py'
        path = path + ':' + '.'.join(test_bits)
        return path

    def finalize(self, result):
        if not result.errors and not result.failures:
           print "ALL GOOD!"
           return

        problems = []
        for error in result.errors:
            problems.append(self.determine_test_path(error))
        for failure in result.failures:
            problems.append(self.determine_test_path(failure))

        print "YOU SHOULD RE-RUN:"
        msg = "nosetests -v --goober "
        if self.prefix:
            msg = self.prefix + msg + '--goober-prefix=' + ','.join(self.env_vars) + ' '
        if self.extra_options:
            msg += str(self.extra_options) + ' '

        print msg + ' '.join(problems)
        <commit_msg>extra options null handle<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from nose.plugins.base import Plugin
from nose.case import Test
import logging
import unittest
import os

log = logging.getLogger(__name__)

class Goober(Plugin):
    """
    After a multiprocess test run, print out a one-line command that will rerun all the failed / error'd tests
    """
    name = "goober"
    score = 2
    enableOpt = "goober"
    activate = "--goober"

    def __init__(self):
        super(Goober, self).__init__()

    def options(self, parser, env):
        parser.add_option('--goober',
                          action='store_true',
                          help="print failed test paths: %s" %
                          (self.help()))
        parser.add_option('--goober-prefix',
                          action='store',
                          type='string',
                          dest='prefix',
                          help="Environment variables to prepend to goober's output. For example, --goober-prefix='LOCALE' will attach 'LOCALE=<os.environ.get('LOCALE')> to 'nosetests -v --goober'")
        parser.add_option('--goober-extra',
                          action='store',
                          help="append additional nose options to goober's output (give comma separated). For example: --goober-extra=--with-xunit,--with-xcover,--xcoverage-file=coverage.xml")

        super(Goober, self).options(parser, env)

    def configure(self, options, conf):
        super(Goober, self).configure(options, conf)
        self.prefix = ''
        self.extra_options = ''
        if not options.prefix:
            return
        self.env_vars = options.prefix.split(',')
        if self.env_vars:
            self.assemble_prefix()
        if options.goober_extra:
            self.extra_options = str(options.goober_extra).replace(","," ")

    def assemble_prefix(self):
        self.prefix += ' '.join(["%s=%s" % (var, os.environ.get(var)) for var in self.env_vars if os.environ.get(var) is not None]) + ' '

    def get_output(self, test):
        try:
            info = test.test
        except AttributeError:
            info = test._id
        bits = str(info).split('.')
        testname = bits.pop()
        path = "/".join(bits)
        path += ".py:%s" % testname
        return path

    def determine_test_path(self, problem):
        """
        When we receive a multiprocess failure/error object, there is a stacktrace and an id. The id is a string representation of the path to the test that failed. But, no determination is made about whether the test that failed was part of a test class, or a standalone test.

        We will break that id into pieces, splitting on '.' - we will re-assemble that id as a path, from the begnning, and search for it in the stacktrace. The first time we don't find it marks the end of the file path, and the beginning of the name of the test class/specific test. We keep the dots separating that part.
        """
        test, trace = problem
        try:
            test_id = test.id()
        except AttributeError:
            test_id = test._id

        test_bits = str(test_id).split('.')
        path = ''
        while path in trace:
            bit = test_bits.pop(0)
            path += bit + "/"
        path = path.rstrip('/') + '.py'
        path = path + ':' + '.'.join(test_bits)
        return path

    def finalize(self, result):
        if not result.errors and not result.failures:
           print "ALL GOOD!"
           return

        problems = []
        for error in result.errors:
            problems.append(self.determine_test_path(error))
        for failure in result.failures:
            problems.append(self.determine_test_path(failure))

        print "YOU SHOULD RE-RUN:"
        msg = "nosetests -v --goober "
        if self.prefix:
            msg = self.prefix + msg + '--goober-prefix=' + ','.join(self.env_vars) + ' '
        if self.extra_options:
            msg += str(self.extra_options) + ' '

        print msg + ' '.join(problems)

<|endoftext|>


<commit_before>from __future__ import print_function

from itertools import product, permutations
from collections import defaultdict

import numba.unittest_support as unittest
from numba.targets.base import OverloadSelector
from numba.targets.registry import cpu_target
from numba.targets.imputils import builtin_registry, RegistryLoader
from numba import types

class TestOverloadSelector(unittest.TestCase):
    def test_select_and_sort_1(self):
        os = OverloadSelector()
        os.append(1, (types.Any, types.Boolean))
        os.append(2, (types.Boolean, types.Integer))
        os.append(3, (types.Boolean, types.Any))
        os.append(4, (types.Boolean, types.Boolean))
        compats = os._select_compatible((types.boolean, types.boolean))
        self.assertEqual(len(compats), 3)
        ordered, scoring = os._sort_signatures(compats)
        self.assertEqual(len(ordered), 3)
        self.assertEqual(len(scoring), 3)
        self.assertEqual(ordered[0], (types.Boolean, types.Boolean))
        self.assertEqual(scoring[types.Boolean, types.Boolean], 0)
        self.assertEqual(scoring[types.Boolean, types.Any], 1)
        self.assertEqual(scoring[types.Any, types.Boolean], 1)

    def test_select_and_sort_2(self):
        os = OverloadSelector()
        os.append(1, (types.Container,))
        os.append(2, (types.Sequence,))
        os.append(3, (types.MutableSequence,))
        os.append(4, (types.List,))
        compats = os._select_compatible((types.List,))
        self.assertEqual(len(compats), 4)
        ordered, scoring = os._sort_signatures(compats)
        self.assertEqual(len(ordered), 4)
        self.assertEqual(len(scoring), 4)
        self.assertEqual(ordered[0], (types.List,))
        self.assertEqual(scoring[(types.List,)], 0)
        self.assertEqual(scoring[(types.MutableSequence,)], 1)
        self.assertEqual(scoring[(types.Sequence,)], 2)
        self.assertEqual(scoring[(types.Container,)], 3)

    def test_match(self):
        os = OverloadSelector()
        self.assertTrue(os._match(formal=types.Boolean, actual=types.boolean))
        self.assertTrue(os._match(formal=types.Boolean, actual=types.Boolean))
        # test subclass
        self.assertTrue(issubclass(types.Sequence, types.Container))
        self.assertTrue(os._match(formal=types.Container,
                                  actual=types.Sequence))
        self.assertFalse(os._match(formal=types.Sequence,
                                   actual=types.Container))
        # test any
        self.assertTrue(os._match(formal=types.Any, actual=types.Any))
        self.assertTrue(os._match(formal=types.Any, actual=types.Container))
        self.assertFalse(os._match(formal=types.Container, actual=types.Any))

    def test_ambiguous_detection(self):
        os = OverloadSelector()
        # unambiguous signatures
        os.append(1, (types.Any, types.Boolean))
        os.append(2, (types.Integer, types.Boolean))
        self.assertEqual(os.find((types.boolean, types.boolean)), 1)
        # not implemented
        with self.assertRaises(NotImplementedError) as raises:
            os.find((types.boolean, types.int32))
        # generic
        os.append(3, (types.Any, types.Any))
        self.assertEqual(os.find((types.boolean, types.int32)), 3)
        self.assertEqual(os.find((types.boolean, types.boolean)), 1)
        # add ambiguous signature; can match (bool, any) and (any, bool)
        os.append(4, (types.Boolean, types.Any))
        with self.assertRaises(TypeError) as raises:
            os.find((types.boolean, types.boolean))
        self.assertIn('2 ambiguous signatures', str(raises.exception))
        # disambiguous
        os.append(5, (types.boolean, types.boolean))
        self.assertEqual(os.find((types.boolean, types.boolean)), 5)

    def test_subclass_specialization(self):
        os = OverloadSelector()
        self.assertTrue(issubclass(types.Sequence, types.Container))
        os.append(1, (types.Container, types.Container,))
        lstty = types.List(types.boolean)
        self.assertEqual(os.find((lstty, lstty)), 1)
        os.append(2, (types.Container, types.Sequence,))
        self.assertEqual(os.find((lstty, lstty)), 2)

class TestValidateOverloads(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # ensure all impls are loaded
        cpu_target.target_context.refresh()

    def create_overload_selector(self, kind):
        os = OverloadSelector()
        loader = RegistryLoader(builtin_registry)
        for impl, sig in loader.new_registrations(kind):
            os.append(impl, sig)
        return os

    def test_ambiguous_casts(self):
        os = self.create_overload_selector(kind='casts')
        all_types = set(t for sig, impl in os.versions for t in sig)
        # ensure there are no ambiguous cast overloads
        for sig in permutations(all_types, r=2):
            try:
                os.find(sig)
            except NotImplementedError:
                pass   # ignore not implemented cast

if __name__ == '__main__':
    unittest.main()
<commit_msg>Add testing for ambiguous function overloads<commit_after>from __future__ import print_function

from itertools import product, permutations
from collections import defaultdict

import numba.unittest_support as unittest
from numba.targets.base import OverloadSelector
from numba.targets.registry import cpu_target
from numba.targets.imputils import builtin_registry, RegistryLoader
from numba import types

class TestOverloadSelector(unittest.TestCase):
    def test_select_and_sort_1(self):
        os = OverloadSelector()
        os.append(1, (types.Any, types.Boolean))
        os.append(2, (types.Boolean, types.Integer))
        os.append(3, (types.Boolean, types.Any))
        os.append(4, (types.Boolean, types.Boolean))
        compats = os._select_compatible((types.boolean, types.boolean))
        self.assertEqual(len(compats), 3)
        ordered, scoring = os._sort_signatures(compats)
        self.assertEqual(len(ordered), 3)
        self.assertEqual(len(scoring), 3)
        self.assertEqual(ordered[0], (types.Boolean, types.Boolean))
        self.assertEqual(scoring[types.Boolean, types.Boolean], 0)
        self.assertEqual(scoring[types.Boolean, types.Any], 1)
        self.assertEqual(scoring[types.Any, types.Boolean], 1)

    def test_select_and_sort_2(self):
        os = OverloadSelector()
        os.append(1, (types.Container,))
        os.append(2, (types.Sequence,))
        os.append(3, (types.MutableSequence,))
        os.append(4, (types.List,))
        compats = os._select_compatible((types.List,))
        self.assertEqual(len(compats), 4)
        ordered, scoring = os._sort_signatures(compats)
        self.assertEqual(len(ordered), 4)
        self.assertEqual(len(scoring), 4)
        self.assertEqual(ordered[0], (types.List,))
        self.assertEqual(scoring[(types.List,)], 0)
        self.assertEqual(scoring[(types.MutableSequence,)], 1)
        self.assertEqual(scoring[(types.Sequence,)], 2)
        self.assertEqual(scoring[(types.Container,)], 3)

    def test_match(self):
        os = OverloadSelector()
        self.assertTrue(os._match(formal=types.Boolean, actual=types.boolean))
        self.assertTrue(os._match(formal=types.Boolean, actual=types.Boolean))
        # test subclass
        self.assertTrue(issubclass(types.Sequence, types.Container))
        self.assertTrue(os._match(formal=types.Container,
                                  actual=types.Sequence))
        self.assertFalse(os._match(formal=types.Sequence,
                                   actual=types.Container))
        # test any
        self.assertTrue(os._match(formal=types.Any, actual=types.Any))
        self.assertTrue(os._match(formal=types.Any, actual=types.Container))
        self.assertFalse(os._match(formal=types.Container, actual=types.Any))

    def test_ambiguous_detection(self):
        os = OverloadSelector()
        # unambiguous signatures
        os.append(1, (types.Any, types.Boolean))
        os.append(2, (types.Integer, types.Boolean))
        self.assertEqual(os.find((types.boolean, types.boolean)), 1)
        # not implemented
        with self.assertRaises(NotImplementedError) as raises:
            os.find((types.boolean, types.int32))
        # generic
        os.append(3, (types.Any, types.Any))
        self.assertEqual(os.find((types.boolean, types.int32)), 3)
        self.assertEqual(os.find((types.boolean, types.boolean)), 1)
        # add ambiguous signature; can match (bool, any) and (any, bool)
        os.append(4, (types.Boolean, types.Any))
        with self.assertRaises(TypeError) as raises:
            os.find((types.boolean, types.boolean))
        self.assertIn('2 ambiguous signatures', str(raises.exception))
        # disambiguous
        os.append(5, (types.boolean, types.boolean))
        self.assertEqual(os.find((types.boolean, types.boolean)), 5)

    def test_subclass_specialization(self):
        os = OverloadSelector()
        self.assertTrue(issubclass(types.Sequence, types.Container))
        os.append(1, (types.Container, types.Container,))
        lstty = types.List(types.boolean)
        self.assertEqual(os.find((lstty, lstty)), 1)
        os.append(2, (types.Container, types.Sequence,))
        self.assertEqual(os.find((lstty, lstty)), 2)

class TestAmbiguousOverloads(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # ensure all impls are loaded
        cpu_target.target_context.refresh()

    def create_overload_selector(self, kind):
        os = OverloadSelector()
        loader = RegistryLoader(builtin_registry)
        for impl, sig in loader.new_registrations(kind):
            os.append(impl, sig)
        return os

    def test_ambiguous_casts(self):
        os = self.create_overload_selector(kind='casts')
        all_types = set(t for sig, impl in os.versions for t in sig)
        # ensure there are no ambiguous cast overloads
        # note: using permutations to avoid testing cast to the same type
        for sig in permutations(all_types, r=2):
            try:
                os.find(sig)
            except NotImplementedError:
                pass   # ignore not implemented cast

    def test_ambiguous_functions(self):
        loader = RegistryLoader(builtin_registry)
        selectors = defaultdict(OverloadSelector)
        for impl, fn, sig in loader.new_registrations('functions'):
            os = selectors[fn]
            os.append(impl, sig)

        for fn, os in selectors.items():
            all_types = set(t for sig, impl in os.versions for t in sig)
            # ensure there are no ambiguous overloads
            for sig in product(all_types, all_types):
                try:
                    os.find(sig)
                except NotImplementedError:
                    pass   # ignore not implemented cast

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


<commit_before># -*- coding: utf-8 -*-
from __future__ import with_statement

import inspect
import os
import sys
import time

from fabric import api as fab
from fabric.contrib import files as fab_files
from fab_deploy.system import ssh_add_key
from fab_deploy.utils import run_as

from taskset import TaskSet, task

from base import _, OS, Debian, Ubuntu, Ubuntu104, Daemon, CommandNamespace
from utils import upload_template, upload_first

def command(func=None, namespace=None, same_name=False, aliases=()):
    def decorator(view_func):
        #@wraps(view_func)
        def f(self, *args, **kwargs):

            ns_obj = getattr(fab.env, namespace, None)
            if ns_obj is None:
                ns_obj = CommandNamespace.get(namespace)()
            return getattr(ns_obj, view_func.__name__)(*args, **kwargs)

        attr_name = '_'.join(filter(None, (namespace,
                                           view_func.__name__)))

        setattr(Deployment, attr_name, f)
        if same_name:
            setattr(Deployment, view_func.__name__, f)

        for alias in aliases:
            setattr(Deployment, alias, f)
        return view_func

    if namespace is None:
        #XXX need more clever solution
        namespace = inspect.stack()[1][0].f_locals['namespace']

    if func:
        return decorator(func)
    return decorator

class Deployment(TaskSet):

    def __init__(self):
        self.expose_to(self.__module__)

    def _is_task(self, func):
        return inspect.ismethod(func) and not func.func_name.startswith('_')

    def _task_for_method(self, method):
        return method

    def ssh_add_key(self, pub_key_file):
        """ Adds a ssh key from passed file to user's
            authorized_keys on server. """
        with open(os.path.normpath(pub_key_file), 'rt') as f:
            ssh_key = f.read()
        if fab.env.user == 'root':
            ssh_dir = '/root/.ssh'
        else:
            if 'home_dir' in fab.env:
                ssh_dir = _('%(home_dir)s/.ssh')
            else:
                ssh_dir = _('/home/%(user)s/.ssh')

        remote_os = fab.env.os
        remote_os.mkdir(ssh_dir)
        fab_files.append('%s/authorized_keys' % ssh_dir, ssh_key)

        with fab.settings(warn_only=True): # no chmod in system
           remote_os.set_permissions(ssh_dir, pattern='700')
           remote_os.set_permissions('%s/authorized_keys' % ssh_dir,
                                     pattern='600')

    @run_as('root')
    def update_rsa_key(self, pub_key_file):
        """ Root adds a ssh key from passed file to user's
            authorized_keys on server."""

        self.ssh_add_key(pub_key_file)

    def os_add_user(self):
        fab.env.os.add_user(fab.env.user)

    def add_deploy_key(self):
        fab.run('ssh-keygen -q')
        output = fab.run('cat ~/.ssh/id_rsa.pub')
        fab.local('echo %s > deploy_key' % output)

    def add_user(self):
        self.os_add_user()
        self.update_rsa_key()

    def backup(self):
        fab.env.db.backup()
        fab.env.project.backup()

    def update_env(self):
        fab.env.project.install()
        if 'provider' in fab.env and fab.env.provider.can_addpackages:
            fab.env.project.install_development_libraries()
            fab.env.project.install_setuptools()
            fab.env.project.install_virtualenv()

    def create_virtual_env(self):
        self.update_env()
        self.create_env()
        self.deploy()
        self.update_virtual_env()

    @run_as('root')
    def sudo_configure(self):

        upload_template('sudoers/speedydeploy',
                        '/etc/sudoers.d/speedydeploy',
                        fab.env,
                        use_sudo=True,
                        use_jinja=True)

        fab.env.os.change_mode('/etc/sudoers.d/speedydeploy', '0440')

    @run_as('root')
    def speedydeploy_configure(self):
        fab.sudo('groupadd -f speedydeploy')
        fab.sudo('usermod -a -G speedydeploy %s' % fab.env.user)

    def create(self, key=None):

        if 'provider' in fab.env and fab.env.provider.can_adduser:
            if key:
                self.update_rsa_key(key) # for root

            self.os_add_user()
            #self.os_add_group('speedydeploy')
            #self.os_add_user_to_group(fab.env.user, 'speedydeploy')

            if key:
                self.ssh_add_key(key)

            fab.run(_('echo root > %(remote_dir)s/.forward'))

        if 'db' in fab.env:
            self.db_create_user(fab.env.user, fab.env.db_pass)
            self.db_create_db(fab.env.user, fab.env.user, fab.env.db_pass)

        self.create_virtual_env()

    def update(self):
        project = fab.env.project

        if project.use_django:
            project.django.reload()

        if project.use_celery:
            self.celery_configure()
        if project.use_sphinxsearch:
            self.sphinxsearch_configure()
        if project.use_memcache:
            self.memcache_configure()

        if project.use_server:
            self.server_configure()
            self.server_reload()
<commit_msg>added full_update command<commit_after># -*- coding: utf-8 -*-
from __future__ import with_statement

import inspect
import os
import sys
import time

from fabric import api as fab
from fabric.contrib import files as fab_files
from fab_deploy.system import ssh_add_key
from fab_deploy.utils import run_as

from taskset import TaskSet, task

from base import _, OS, Debian, Ubuntu, Ubuntu104, Daemon, CommandNamespace
from utils import upload_template, upload_first

def command(func=None, namespace=None, same_name=False, aliases=()):
    def decorator(view_func):
        #@wraps(view_func)
        def f(self, *args, **kwargs):

            ns_obj = getattr(fab.env, namespace, None)
            if ns_obj is None:
                ns_obj = CommandNamespace.get(namespace)()
            return getattr(ns_obj, view_func.__name__)(*args, **kwargs)

        attr_name = '_'.join(filter(None, (namespace,
                                           view_func.__name__)))

        setattr(Deployment, attr_name, f)
        if same_name:
            setattr(Deployment, view_func.__name__, f)

        for alias in aliases:
            setattr(Deployment, alias, f)
        return view_func

    if namespace is None:
        #XXX need more clever solution
        namespace = inspect.stack()[1][0].f_locals['namespace']

    if func:
        return decorator(func)
    return decorator

class Deployment(TaskSet):

    def __init__(self):
        self.expose_to(self.__module__)

    def _is_task(self, func):
        return inspect.ismethod(func) and not func.func_name.startswith('_')

    def _task_for_method(self, method):
        return method

    def ssh_add_key(self, pub_key_file):
        """ Adds a ssh key from passed file to user's
            authorized_keys on server. """
        with open(os.path.normpath(pub_key_file), 'rt') as f:
            ssh_key = f.read()
        if fab.env.user == 'root':
            ssh_dir = '/root/.ssh'
        else:
            if 'home_dir' in fab.env:
                ssh_dir = _('%(home_dir)s/.ssh')
            else:
                ssh_dir = _('/home/%(user)s/.ssh')

        remote_os = fab.env.os
        remote_os.mkdir(ssh_dir)
        fab_files.append('%s/authorized_keys' % ssh_dir, ssh_key)

        with fab.settings(warn_only=True): # no chmod in system
           remote_os.set_permissions(ssh_dir, pattern='700')
           remote_os.set_permissions('%s/authorized_keys' % ssh_dir,
                                     pattern='600')

    @run_as('root')
    def update_rsa_key(self, pub_key_file):
        """ Root adds a ssh key from passed file to user's
            authorized_keys on server."""

        self.ssh_add_key(pub_key_file)

    def os_add_user(self):
        fab.env.os.add_user(fab.env.user)

    def add_deploy_key(self):
        fab.run('ssh-keygen -q')
        output = fab.run('cat ~/.ssh/id_rsa.pub')
        fab.local('echo %s > deploy_key' % output)

    def add_user(self):
        self.os_add_user()
        self.update_rsa_key()

    def backup(self):
        fab.env.db.backup()
        fab.env.project.backup()

    def update_env(self):
        fab.env.project.install()
        if 'provider' in fab.env and fab.env.provider.can_addpackages:
            fab.env.project.install_development_libraries()
            fab.env.project.install_setuptools()
            fab.env.project.install_virtualenv()

    def create_virtual_env(self):
        self.update_env()
        self.create_env()
        self.deploy()
        self.update_virtual_env()

    @run_as('root')
    def sudo_configure(self):

        upload_template('sudoers/speedydeploy',
                        '/etc/sudoers.d/speedydeploy',
                        fab.env,
                        use_sudo=True,
                        use_jinja=True)

        fab.env.os.change_mode('/etc/sudoers.d/speedydeploy', '0440')

    @run_as('root')
    def speedydeploy_configure(self):
        fab.sudo('groupadd -f speedydeploy')
        fab.sudo('usermod -a -G speedydeploy %s' % fab.env.user)

    def create(self, key=None):

        if 'provider' in fab.env and fab.env.provider.can_adduser:
            if key:
                self.update_rsa_key(key) # for root

            self.os_add_user()
            #self.os_add_group('speedydeploy')
            #self.os_add_user_to_group(fab.env.user, 'speedydeploy')

            if key:
                self.ssh_add_key(key)

            fab.run(_('echo root > %(remote_dir)s/.forward'))

        if 'db' in fab.env:
            self.db_create_user(fab.env.user, fab.env.db_pass)
            self.db_create_db(fab.env.user, fab.env.user, fab.env.db_pass)

        self.create_virtual_env()

    def update(self):
        project = fab.env.project

        if project.use_django:
            project.django.reload()

        if project.use_celery:
            self.celery_configure()
        if project.use_sphinxsearch:
            self.sphinxsearch_configure()
        if project.use_memcache:
            self.memcache_configure()

        if project.use_server:
            self.server_configure()
            self.server_reload()

    def full_update(self):
        self.deploy()
        self.update_reqs()
        self.update()
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import sys

PROCEDURES = {
    'accumulate_custom.cpp' : {
        'title'             : "accumulate --- default",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/accumulate',
        'procedures'        : ["accumulate_custom_epi8", "accumulate_custom_epi32"],
    },

    'accumulate_default.cpp' : {
        'title'             : "accumulate --- custom",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/accumulate',
        'procedures'        : ["accumulate_epi8", "accumulate_epi32"],
    },

    'all_of.cpp'            : {
        'title'             : "all_of",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/all_any_none_of',
        'procedures'        : ["all_of_epi8", "all_of_epi32"],
    },

    'any_of.cpp'            : {
        'title'             : "any_of",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/all_any_none_of',
        'procedures'        : ["any_of_epi8", "any_of_epi32"],
    },

    'copy.cpp'              : {
        'title'             : "copy",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/copy',
        'procedures'        : ["copy_epi8", "copy_epi32"],
    },

    'copy_if.cpp'           : {
        'title'             : "copy_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/copy',
        'procedures'        : ["copy_if_epi8", "copy_if_epi32"],
    },

    'count.cpp'             : {
        'title'             : "count",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/count',
        'procedures'        : ["count_epi8", "count_epi32"],
    },

    'count_if.cpp'          : {
        'title'             : "count_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/count',
        'procedures'        : ["count_if_epi8", "count_if_epi32"],
    },

    'fill.cpp'              : {
        'title'             : "fill",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/fill',
        'procedures'        : ["fill_epi8", "fill_epi32"],
    },

    'find.cpp'              : {
        'title'             : "find",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/find',
        'procedures'        : ["find_epi8", "find_epi32"],
    },

    'find_if.cpp'           : {
        'title'             : "find_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/find',
        'procedures'        : ["find_if_epi8", "find_if_epi32"],
    },

    'is_sorted.cpp'         : {
        'title'             : "is_sorted",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/is_sorted',
        'procedures'        : ["is_sorted_epi8", "is_sorted_epi32"],
    },

    'none_of.cpp'           : {
        'title'             : "none_of",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/all_any_none_of',
        'procedures'        : ["none_of_epi8", "none_of_epi32"],
    },

    'remove.cpp'            : {
        'title'             : "remove",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/remove',
        'procedures'        : ["remove_epi8", "remove_epi32"],
    },

    'remove_if.cpp'         : {
        'title'             : "remove_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/remove',
        'procedures'        : ["remove_if_epi8", "remove_if_epi32"],
    },

    'replace.cpp'           : {
        'title'             : "replace",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/replace',
        'procedures'        : ["replace_epi8", "replace_epi32"],
    },

    'replace_if.cpp'        : {
        'title'             : "replace_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/replace',
        'procedures'        : ["replace_if_epi8", "replace_if_epi32"],
    },

    'reverse.cpp'           : {
        'title'             : "reverse",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/reverse',
        'procedures'        : ["reverse_epi8", "reverse_epi32"],
    },

    'transform_abs.cpp'     : {
        'title'             : "transform --- abs",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/transform',
        'procedures'        : ["transform_abs_epi8", "transform_abs_epi32"],
    },

    'transform_inc.cpp'     : {
        'title'             : "transform --- increment",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/transform',
        'procedures'        : ["transform_inc_epi8", "transform_inc_epi32"],
    },

    'transform_neg.cpp'    : {
        'title'             : "transform --- negation",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/transform',
        'procedures'        : ["transform_neg_epi8", "transform_neg_epi32"],
    },

    'unique.cpp'            : {
        'title'             : "unique",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/unique',
        'procedures'        : ["unique_epi8", "unique_epi32"],
    },
}

def print_checklist(file):
    procedures = []
    for item in PROCEDURES.values():
        for procedure in item['procedures']:
            procedures.append(procedure)

    def writeln(s):
        file.write(s)
        file.write('\n')

    writeln('compiler: ')
    writeln('cmdline: ')
    writeln('')
    for procedure in sorted(procedures):
        writeln('%s: ??' % procedure)

if __name__ == '__main__':
    print_checklist(sys.stdout)
<commit_msg>Correct order of procedures<commit_after>#!/usr/bin/env python3

import sys

PROCEDURES = {
    'accumulate_default.cpp' : {
        'title'             : "accumulate --- default",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/accumulate',
        'procedures'        : ["accumulate_epi8", "accumulate_epi32"],
    },

    'accumulate_custom.cpp' : {
        'title'             : "accumulate --- custom",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/accumulate',
        'procedures'        : ["accumulate_custom_epi8", "accumulate_custom_epi32"],
    },

    'all_of.cpp'            : {
        'title'             : "all_of",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/all_any_none_of',
        'procedures'        : ["all_of_epi8", "all_of_epi32"],
    },

    'any_of.cpp'            : {
        'title'             : "any_of",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/all_any_none_of',
        'procedures'        : ["any_of_epi8", "any_of_epi32"],
    },

    'copy.cpp'              : {
        'title'             : "copy",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/copy',
        'procedures'        : ["copy_epi8", "copy_epi32"],
    },

    'copy_if.cpp'           : {
        'title'             : "copy_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/copy',
        'procedures'        : ["copy_if_epi8", "copy_if_epi32"],
    },

    'count.cpp'             : {
        'title'             : "count",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/count',
        'procedures'        : ["count_epi8", "count_epi32"],
    },

    'count_if.cpp'          : {
        'title'             : "count_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/count',
        'procedures'        : ["count_if_epi8", "count_if_epi32"],
    },

    'fill.cpp'              : {
        'title'             : "fill",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/fill',
        'procedures'        : ["fill_epi8", "fill_epi32"],
    },

    'find.cpp'              : {
        'title'             : "find",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/find',
        'procedures'        : ["find_epi8", "find_epi32"],
    },

    'find_if.cpp'           : {
        'title'             : "find_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/find',
        'procedures'        : ["find_if_epi8", "find_if_epi32"],
    },

    'is_sorted.cpp'         : {
        'title'             : "is_sorted",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/is_sorted',
        'procedures'        : ["is_sorted_epi8", "is_sorted_epi32"],
    },

    'none_of.cpp'           : {
        'title'             : "none_of",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/all_any_none_of',
        'procedures'        : ["none_of_epi8", "none_of_epi32"],
    },

    'remove.cpp'            : {
        'title'             : "remove",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/remove',
        'procedures'        : ["remove_epi8", "remove_epi32"],
    },

    'remove_if.cpp'         : {
        'title'             : "remove_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/remove',
        'procedures'        : ["remove_if_epi8", "remove_if_epi32"],
    },

    'replace.cpp'           : {
        'title'             : "replace",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/replace',
        'procedures'        : ["replace_epi8", "replace_epi32"],
    },

    'replace_if.cpp'        : {
        'title'             : "replace_if",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/replace',
        'procedures'        : ["replace_if_epi8", "replace_if_epi32"],
    },

    'reverse.cpp'           : {
        'title'             : "reverse",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/reverse',
        'procedures'        : ["reverse_epi8", "reverse_epi32"],
    },

    'transform_abs.cpp'     : {
        'title'             : "transform --- abs",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/transform',
        'procedures'        : ["transform_abs_epi8", "transform_abs_epi32"],
    },

    'transform_inc.cpp'     : {
        'title'             : "transform --- increment",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/transform',
        'procedures'        : ["transform_inc_epi8", "transform_inc_epi32"],
    },

    'transform_neg.cpp'    : {
        'title'             : "transform --- negation",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/transform',
        'procedures'        : ["transform_neg_epi8", "transform_neg_epi32"],
    },

    'unique.cpp'            : {
        'title'             : "unique",
        'link'              : 'https://en.cppreference.com/w/cpp/algorithm/unique',
        'procedures'        : ["unique_epi8", "unique_epi32"],
    },
}

def print_checklist(file):
    procedures = []
    for item in PROCEDURES.values():
        for procedure in item['procedures']:
            procedures.append(procedure)

    def writeln(s):
        file.write(s)
        file.write('\n')

    writeln('compiler: ')
    writeln('cmdline: ')
    writeln('')
    for procedure in sorted(procedures):
        writeln('%s: ??' % procedure)

if __name__ == '__main__':
    print_checklist(sys.stdout)
<|endoftext|>


<commit_before>"""
Tests for splitter objects.
"""
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

__author__ = "Bharath Ramsundar, Aneesh Pappu"
__copyright__ = "Copyright 2016, Stanford University"
__license__ = "GPL"

from deepchem.splits import RandomSplitter
from deepchem.splits import ScaffoldSplitter
from deepchem.splits import StratifiedSplitter
from deepchem.datasets.tests import TestDatasetAPI
import pandas as pd

class TestSplitters(TestDatasetAPI):
    """
    Test some basic splitters.
    """

    def test_singletask_random_split(self):
        """
        Test singletask RandomSplitter class.
        """
        solubility_dataset = self.load_solubility_data()
        random_splitter = RandomSplitter()
        train_data, valid_data, test_data = \
            random_splitter.train_valid_test_split(
                solubility_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1)
        assert len(train_data) == 8
        assert len(valid_data) == 1
        assert len(test_data) == 1

    def test_singletask_scaffold_split(self):
        """
        Test singletask ScaffoldSplitter class.
        """
        solubility_dataset = self.load_solubility_data()
        scaffold_splitter = ScaffoldSplitter()
        train_data, valid_data, test_data = \
            scaffold_splitter.train_valid_test_split(
                solubility_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1)
        assert len(train_data) == 8
        assert len(valid_data) == 1
        assert len(test_data) == 1

    def test_multitask_random_split(self):
        """
        Test multitask RandomSplitter class.
        """
        multitask_dataset = self.load_multitask_data()
        random_splitter = RandomSplitter()
        train_data, valid_data, test_data = \
            random_splitter.train_valid_test_split(
                multitask_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1)
        assert len(train_data) == 8
        assert len(valid_data) == 1
        assert len(test_data) == 1

    def test_multitask_scaffold_split(self):
        """
        Test multitask ScaffoldSplitter class.
        """
        multitask_dataset = self.load_multitask_data()
        scaffold_splitter = ScaffoldSplitter()
        train_data, valid_data, test_data = \
            scaffold_splitter.train_valid_test_split(
                multitask_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1)
        assert len(train_data) == 8
        assert len(valid_data) == 1
        assert len(test_data) == 1

    def test_stratified_multitask_split(self):
        """
        Test multitask StratifiedSplitter class
        """
        sparse_dataset = self.load_sparse_multitask_dataset()
        stratified_splitter = StratifiedSplitter()
        train_data, valid_data, test_data = \
            stratified_splitter.train_valid_test_split(
                sparse_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1
            )

        datasets = [train_data, valid_data, test_data]
        datasetIndex = 0
        for dataset in datasets:
            np_list = dataset.to_numpy()
            y = np_list[1]
            # verify that each task in the train dataset has some hits
            y_df = pd.DataFrame(data=y)
            totalRows = len(y_df.index)
            for col in y_df:
                column = y_df[col]
                NaN_count = column.isnull().sum()
                if NaN_count == totalRows:
                    print("fail -- one column doesn't have results")
                    if datasetIndex == 0:
                        print("train_data failed")
                    elif datasetIndex == 1:
                        print("valid_data failed")
                    elif datasetIndex == 2:
                        print("test_data failed")
                    assert NaN_count != totalRows
            if datasetIndex == 0:
                print("train_data passed")
            elif datasetIndex == 1:
                print("valid_data passed")
            elif datasetIndex == 2:
                print("test_data passed")
            datasetIndex+=1
        print("end of stratified test")
        assert 1 == 1
<commit_msg>adding test to test for initial sparsity<commit_after>"""
Tests for splitter objects.
"""
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

__author__ = "Bharath Ramsundar, Aneesh Pappu"
__copyright__ = "Copyright 2016, Stanford University"
__license__ = "GPL"

from deepchem.splits import RandomSplitter
from deepchem.splits import ScaffoldSplitter
from deepchem.splits import StratifiedSplitter
from deepchem.datasets.tests import TestDatasetAPI
import pandas as pd

class TestSplitters(TestDatasetAPI):
    """
    Test some basic splitters.
    """

    def test_singletask_random_split(self):
        """
        Test singletask RandomSplitter class.
        """
        solubility_dataset = self.load_solubility_data()
        random_splitter = RandomSplitter()
        train_data, valid_data, test_data = \
            random_splitter.train_valid_test_split(
                solubility_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1)
        assert len(train_data) == 8
        assert len(valid_data) == 1
        assert len(test_data) == 1

    def test_singletask_scaffold_split(self):
        """
        Test singletask ScaffoldSplitter class.
        """
        solubility_dataset = self.load_solubility_data()
        scaffold_splitter = ScaffoldSplitter()
        train_data, valid_data, test_data = \
            scaffold_splitter.train_valid_test_split(
                solubility_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1)
        assert len(train_data) == 8
        assert len(valid_data) == 1
        assert len(test_data) == 1

    def test_multitask_random_split(self):
        """
        Test multitask RandomSplitter class.
        """
        multitask_dataset = self.load_multitask_data()
        random_splitter = RandomSplitter()
        train_data, valid_data, test_data = \
            random_splitter.train_valid_test_split(
                multitask_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1)
        assert len(train_data) == 8
        assert len(valid_data) == 1
        assert len(test_data) == 1

    def test_multitask_scaffold_split(self):
        """
        Test multitask ScaffoldSplitter class.
        """
        multitask_dataset = self.load_multitask_data()
        scaffold_splitter = ScaffoldSplitter()
        train_data, valid_data, test_data = \
            scaffold_splitter.train_valid_test_split(
                multitask_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1)
        assert len(train_data) == 8
        assert len(valid_data) == 1
        assert len(test_data) == 1

    def test_stratified_multitask_split(self):
        """
        Test multitask StratifiedSplitter class
        """
        #ensure sparse dataset is actually sparse
        sparse_dataset = self.load_sparse_multitask_dataset()
        sparse_np_list = sparse_dataset.to_numpy()
        sparse_np = sparse_np_list[1]
        sparse_df = pd.DataFrame(data = sparse_np)
        total_rows = len(sparse_df.index)
        sparse_flag = False
        for col in sparse_df:
            column = sparse_df[col]
            NaN_count = column.isnull().sum()
            if NaN_count == total_rows:
                print("good -- one column doesn't have results")
                sparse_flag = True
                assert NaN_count == total_rows
                break
        if not sparse_flag:
            print("datset isn't sparse")
            assert sparse_flag is True
        else:
            print("dataset is sparse")

        stratified_splitter = StratifiedSplitter()
        train_data, valid_data, test_data = \
            stratified_splitter.train_valid_test_split(
                sparse_dataset,
                self.train_dir, self.valid_dir, self.test_dir,
                frac_train=0.8, frac_valid=0.1, frac_test=0.1
            )

        datasets = [train_data, valid_data, test_data]
        datasetIndex = 0
        for dataset in datasets:
            np_list = dataset.to_numpy()
            y = np_list[1]
            # verify that each task in the train dataset has some hits
            y_df = pd.DataFrame(data=y)
            totalRows = len(y_df.index)
            for col in y_df:
                column = y_df[col]
                NaN_count = column.isnull().sum()
                if NaN_count == totalRows:
                    print("fail -- one column doesn't have results")
                    if datasetIndex == 0:
                        print("train_data failed")
                    elif datasetIndex == 1:
                        print("valid_data failed")
                    elif datasetIndex == 2:
                        print("test_data failed")
                    assert NaN_count != totalRows
            if datasetIndex == 0:
                print("train_data passed")
            elif datasetIndex == 1:
                print("valid_data passed")
            elif datasetIndex == 2:
                print("test_data passed")
            datasetIndex+=1
        print("end of stratified test")
        assert 1 == 1
<|endoftext|>