<commit_before>import collections
import functools
import inspect
import sys
import threading
import os

import six
# noinspection PyUnresolvedReferences
from cheap_repr import cheap_repr, find_repr_function

from snoop.utils import my_cheap_repr, NO_ASTTOKENS
from . import utils, pycompat
from .formatting import Event
from .variables import CommonVariable, Exploding, BaseVariable

try:
    from django.db.models import QuerySet
except ImportError:
    class QuerySet(object):
        pass

find_repr_function(six.text_type).maxparts = 100
find_repr_function(six.binary_type).maxparts = 100
find_repr_function(object).maxparts = 100
find_repr_function(int).maxparts = 999999
cheap_repr.suppression_threshold = 999999

class FrameInfo(object):
    def __init__(self, frame):
        self.frame = frame
        self.local_reprs = {}
        self.last_line_no = frame.f_lineno
        self.comprehension_variables = collections.OrderedDict()

    def update_variables(self, watch, watch_extras, event):
        self.last_line_no = self.frame.f_lineno
        old_local_reprs = self.local_reprs
        self.local_reprs = self.get_local_reprs(watch, watch_extras)

        if utils.is_comprehension_frame(self.frame):
            for name, value_repr in self.local_reprs.items():
                values = self.comprehension_variables.setdefault(name, [])
                if not values or values[-1] != value_repr:
                    values.append(value_repr)
                    values[:] = utils.truncate_list(values, 11)
            if event in ('return', 'exception'):
                return [
                    (name, ', '.join(values))
                    for name, values in self.comprehension_variables.items()
                ]
            else:
                return []

        variables = []
        for name, value_repr in self.local_reprs.items():
            if name not in old_local_reprs or old_local_reprs[name] != value_repr:
                variables.append((name, value_repr))
        return variables

    def get_local_reprs(self, watch, watch_extras):
        frame = self.frame
        code = frame.f_code
        vars_order = code.co_varnames + code.co_cellvars + code.co_freevars + tuple(frame.f_locals.keys())

        result_items = [(key, value) for key, value in frame.f_locals.items()]
        result_items.sort(key=lambda key_value: vars_order.index(key_value[0]))

        for variable in watch:
            result_items += sorted(variable.items(frame))

        result = collections.OrderedDict()
        for source, value in result_items:
            result[source] = my_cheap_repr(value)
            for extra in watch_extras:

                try:
                    pair = extra(source, value)
                except Exception:
                    pass
                else:
                    if pair is not None:
                        extra_source, extra_value = pair
                        result[extra_source] = extra_value

        return result

def len_watch(source, value):
    if isinstance(value, QuerySet):
        # Getting the length of a Django queryset evaluates it
        return None

    length = len(value)
    if (
            (isinstance(value, six.string_types)
             and length < 50) or
            (isinstance(value, (collections.Mapping, collections.Set, collections.Sequence))
             and length == 0)
    ):
        return None

    return 'len({})'.format(source), length

def shape_watch(source, value):
    shape = value.shape
    if inspect.ismethod(shape):
        return None
    return '{}.shape'.format(source), shape

thread_global = threading.local()
internal_directories = (os.path.dirname(shape_watch.__code__.co_filename),)

try:
    import birdseye.tracer
except ImportError:
    pass
else:
    internal_directories += (os.path.dirname(birdseye.BirdsEye.trace_function.__code__.co_filename),)

class TracerMeta(type):
    def __new__(mcs, *args, **kwargs):
        result = super(TracerMeta, mcs).__new__(mcs, *args, **kwargs)
        result.default = result()
        return result

    def __call__(cls, *args, **kwargs):
        if len(args) == 1 and callable(args[0]) and not kwargs:
            return cls.default(args[0])
        else:
            return super(TracerMeta, cls).__call__(*args, **kwargs)

    def __enter__(self):
        return self.default.__enter__(context=1)

    def __exit__(self, *args):
        return self.default.__exit__(*args, context=1)

@six.add_metaclass(TracerMeta)
class Tracer(object):
    '''
    Snoop on the function, writing everything it's doing to stderr.

    This is useful for debugging.

    When you decorate a function with `@snoop()`
    or wrap a block of code in `with snoop():`, you'll get a log of
    every line that ran in the function and a play-by-play of every local
    variable that changed.

    If stderr is not easily accessible for you, you can redirect the output to
    a file::

        @snoop('/my/log/file.log')

    See values of some expressions that aren't local variables::

        @snoop(watch=('foo.bar', 'self.x["whatever"]'))

    Expand values to see all their attributes or items of lists/dictionaries:

        @snoop(watch_explode=('foo', 'self'))

    (see Advanced Usage in the README for more control)

    Show snoop lines for functions that your function calls::

        @snoop(depth=2)

    Start all snoop lines with a prefix, to grep for them easily::

        @snoop(prefix='ZZZ ')
    '''

    def __init__(
            self,
            watch=(),
            watch_explode=(),
            watch_extras=(),
            replace_watch_extras=None,
            depth=1,
    ):
        self.watch = [
            v if isinstance(v, BaseVariable) else CommonVariable(v)
            for v in utils.ensure_tuple(watch)
         ] + [
             v if isinstance(v, BaseVariable) else Exploding(v)
             for v in utils.ensure_tuple(watch_explode)
        ]
        if replace_watch_extras is not None:
            self.watch_extras = utils.ensure_tuple(replace_watch_extras)
        else:
            self.watch_extras = (len_watch, shape_watch) + utils.ensure_tuple(watch_extras)
        self.frame_infos = {}
        self.depth = depth
        assert self.depth >= 1
        self.target_codes = set()
        self.target_frames = set()

    def __call__(self, function):
        self.target_codes.add(function.__code__)

        @functools.wraps(function)
        def simple_wrapper(*args, **kwargs):
            with self:
                return function(*args, **kwargs)

        @functools.wraps(function)
        def generator_wrapper(*args, **kwargs):
            gen = function(*args, **kwargs)
            method, incoming = gen.send, None
            while True:
                with self:
                    try:
                        outgoing = method(incoming)
                    except StopIteration:
                        return
                try:
                    method, incoming = gen.send, (yield outgoing)
                except Exception as e:
                    method, incoming = gen.throw, e

        if pycompat.iscoroutinefunction(function):
            raise NotImplementedError("coroutines are not supported, sorry!")
        elif inspect.isgeneratorfunction(function):
            return generator_wrapper
        else:
            return simple_wrapper

    def __enter__(self, context=0):
        if not self.config.enabled:
            return

        calling_frame = sys._getframe(context + 1)
        if not self._is_internal_frame(calling_frame):
            calling_frame.f_trace = self.trace
            self.target_frames.add(calling_frame)
            self.config.last_frame = calling_frame
            self.trace(calling_frame, 'enter', None)

        stack = thread_global.__dict__.setdefault('original_trace_functions', [])
        stack.append(sys.gettrace())
        sys.settrace(self.trace)

    def __exit__(self, exc_type, exc_value, exc_traceback, context=0):
        if not self.config.enabled:
            return

        stack = thread_global.original_trace_functions
        sys.settrace(stack.pop())
        calling_frame = sys._getframe(context + 1)
        self.trace(calling_frame, 'exit', None)
        self.target_frames.discard(calling_frame)
        self.frame_infos.pop(calling_frame, None)

    def _is_internal_frame(self, frame):
        return frame.f_code.co_filename.startswith(internal_directories)

    def trace(self, frame, event, arg):
        if not (frame.f_code in self.target_codes or frame in self.target_frames):
            if (
                    self.depth == 1
                    or self._is_internal_frame(frame)
            ) and not utils.is_comprehension_frame(frame):
                return None
            else:
                candidate = frame
                i = 0
                while True:
                    if utils.is_comprehension_frame(candidate):
                        candidate = candidate.f_back
                        continue
                    i += 1
                    if candidate.f_code in self.target_codes or candidate in self.target_frames:
                        break
                    candidate = candidate.f_back
                    if i >= self.depth or candidate is None or self._is_internal_frame(candidate):
                        return None

        thread_local = self.config.thread_local
        thread_local.__dict__.setdefault('depth', -1)
        frame_info = self.frame_infos.setdefault(frame, FrameInfo(frame))
        if event in ('call', 'enter'):
            thread_local.depth += 1
        elif self.config.last_frame and self.config.last_frame is not frame:
            line_no = self.frame_infos[frame].last_line_no
            trace_event = Event(frame, event, arg, thread_local.depth, line_no=line_no)
            line = self.config.formatter.format_line_only(trace_event)
            self.config.write(line)

        self.config.last_frame = frame

        trace_event = Event(frame, event, arg, thread_local.depth, last_line_no=frame_info.last_line_no)
        if not (frame.f_code.co_name == '<genexpr>' and event not in ('return', 'exception')):
            trace_event.variables = frame_info.update_variables(self.watch, self.watch_extras, event)

        if event in ('return', 'exit'):
            del self.frame_infos[frame]
            thread_local.depth -= 1

        formatted = self.config.formatter.format(trace_event)
        self.config.write(formatted)

        return self.trace

class Spy(object):
    def __init__(self, config):
        self.config = config

    def __call__(self, *args, **kwargs):
        if NO_ASTTOKENS:
            raise Exception("birdseye doesn't support this version of Python")

        try:
            import birdseye
        except ImportError:
            raise Exception("You must install birdseye separately to use spy: pip install birdseye")

        # Decorator without parentheses
        if len(args) == 1 and inspect.isfunction(args[0]) and not kwargs:
            return self._trace(args[0])

        # Decorator with parentheses and perhaps arguments
        def decorator(func):
            return self._trace(func, *args, **kwargs)

        return decorator

    def _trace(self, func, *args, **kwargs):
        # noinspection PyUnresolvedReferences
        from birdseye import eye

        traced = eye(func)
        traced = self.config.snoop(*args, **kwargs)(traced)

        @functools.wraps(func)
        def wrapper(*func_args, **func_kwargs):
            if self.config.enabled:
                final_func = traced
            else:
                final_func = func

            return final_func(*func_args, **func_kwargs)

        return wrapper
        <commit_msg>Use cheap repr of watch extra values<commit_after>import collections
import functools
import inspect
import sys
import threading
import os

import six
# noinspection PyUnresolvedReferences
from cheap_repr import cheap_repr, find_repr_function

from snoop.utils import my_cheap_repr, NO_ASTTOKENS
from . import utils, pycompat
from .formatting import Event
from .variables import CommonVariable, Exploding, BaseVariable

try:
    from django.db.models import QuerySet
except ImportError:
    class QuerySet(object):
        pass

find_repr_function(six.text_type).maxparts = 100
find_repr_function(six.binary_type).maxparts = 100
find_repr_function(object).maxparts = 100
find_repr_function(int).maxparts = 999999
cheap_repr.suppression_threshold = 999999

class FrameInfo(object):
    def __init__(self, frame):
        self.frame = frame
        self.local_reprs = {}
        self.last_line_no = frame.f_lineno
        self.comprehension_variables = collections.OrderedDict()

    def update_variables(self, watch, watch_extras, event):
        self.last_line_no = self.frame.f_lineno
        old_local_reprs = self.local_reprs
        self.local_reprs = self.get_local_reprs(watch, watch_extras)

        if utils.is_comprehension_frame(self.frame):
            for name, value_repr in self.local_reprs.items():
                values = self.comprehension_variables.setdefault(name, [])
                if not values or values[-1] != value_repr:
                    values.append(value_repr)
                    values[:] = utils.truncate_list(values, 11)
            if event in ('return', 'exception'):
                return [
                    (name, ', '.join(values))
                    for name, values in self.comprehension_variables.items()
                ]
            else:
                return []

        variables = []
        for name, value_repr in self.local_reprs.items():
            if name not in old_local_reprs or old_local_reprs[name] != value_repr:
                variables.append((name, value_repr))
        return variables

    def get_local_reprs(self, watch, watch_extras):
        frame = self.frame
        code = frame.f_code
        vars_order = code.co_varnames + code.co_cellvars + code.co_freevars + tuple(frame.f_locals.keys())

        result_items = [(key, value) for key, value in frame.f_locals.items()]
        result_items.sort(key=lambda key_value: vars_order.index(key_value[0]))

        for variable in watch:
            result_items += sorted(variable.items(frame))

        result = collections.OrderedDict()
        for source, value in result_items:
            result[source] = my_cheap_repr(value)
            for extra in watch_extras:
                try:
                    pair = extra(source, value)
                except Exception:
                    pass
                else:
                    if pair is not None:
                        extra_source, extra_value = pair
                        result[extra_source] = my_cheap_repr(extra_value)

        return result

def len_watch(source, value):
    if isinstance(value, QuerySet):
        # Getting the length of a Django queryset evaluates it
        return None

    length = len(value)
    if (
            (isinstance(value, six.string_types)
             and length < 50) or
            (isinstance(value, (collections.Mapping, collections.Set, collections.Sequence))
             and length == 0)
    ):
        return None

    return 'len({})'.format(source), length

def shape_watch(source, value):
    shape = value.shape
    if inspect.ismethod(shape):
        return None
    return '{}.shape'.format(source), shape

thread_global = threading.local()
internal_directories = (os.path.dirname(shape_watch.__code__.co_filename),)

try:
    # noinspection PyUnresolvedReferences
    import birdseye
except ImportError:
    pass
else:
    internal_directories += (os.path.dirname(birdseye.BirdsEye.trace_function.__code__.co_filename),)

class TracerMeta(type):
    def __new__(mcs, *args, **kwargs):
        result = super(TracerMeta, mcs).__new__(mcs, *args, **kwargs)
        result.default = result()
        return result

    def __call__(cls, *args, **kwargs):
        if len(args) == 1 and callable(args[0]) and not kwargs:
            return cls.default(args[0])
        else:
            return super(TracerMeta, cls).__call__(*args, **kwargs)

    def __enter__(self):
        return self.default.__enter__(context=1)

    def __exit__(self, *args):
        return self.default.__exit__(*args, context=1)

@six.add_metaclass(TracerMeta)
class Tracer(object):
    '''
    Snoop on the function, writing everything it's doing to stderr.

    This is useful for debugging.

    When you decorate a function with `@snoop()`
    or wrap a block of code in `with snoop():`, you'll get a log of
    every line that ran in the function and a play-by-play of every local
    variable that changed.

    If stderr is not easily accessible for you, you can redirect the output to
    a file::

        @snoop('/my/log/file.log')

    See values of some expressions that aren't local variables::

        @snoop(watch=('foo.bar', 'self.x["whatever"]'))

    Expand values to see all their attributes or items of lists/dictionaries:

        @snoop(watch_explode=('foo', 'self'))

    (see Advanced Usage in the README for more control)

    Show snoop lines for functions that your function calls::

        @snoop(depth=2)

    Start all snoop lines with a prefix, to grep for them easily::

        @snoop(prefix='ZZZ ')
    '''

    def __init__(
            self,
            watch=(),
            watch_explode=(),
            watch_extras=(),
            replace_watch_extras=None,
            depth=1,
    ):
        self.watch = [
            v if isinstance(v, BaseVariable) else CommonVariable(v)
            for v in utils.ensure_tuple(watch)
         ] + [
             v if isinstance(v, BaseVariable) else Exploding(v)
             for v in utils.ensure_tuple(watch_explode)
        ]
        if replace_watch_extras is not None:
            self.watch_extras = utils.ensure_tuple(replace_watch_extras)
        else:
            self.watch_extras = (len_watch, shape_watch) + utils.ensure_tuple(watch_extras)
        self.frame_infos = {}
        self.depth = depth
        assert self.depth >= 1
        self.target_codes = set()
        self.target_frames = set()

    def __call__(self, function):
        self.target_codes.add(function.__code__)

        @functools.wraps(function)
        def simple_wrapper(*args, **kwargs):
            with self:
                return function(*args, **kwargs)

        @functools.wraps(function)
        def generator_wrapper(*args, **kwargs):
            gen = function(*args, **kwargs)
            method, incoming = gen.send, None
            while True:
                with self:
                    try:
                        outgoing = method(incoming)
                    except StopIteration:
                        return
                try:
                    method, incoming = gen.send, (yield outgoing)
                except Exception as e:
                    method, incoming = gen.throw, e

        if pycompat.iscoroutinefunction(function):
            raise NotImplementedError("coroutines are not supported, sorry!")
        elif inspect.isgeneratorfunction(function):
            return generator_wrapper
        else:
            return simple_wrapper

    def __enter__(self, context=0):
        if not self.config.enabled:
            return

        calling_frame = sys._getframe(context + 1)
        if not self._is_internal_frame(calling_frame):
            calling_frame.f_trace = self.trace
            self.target_frames.add(calling_frame)
            self.config.last_frame = calling_frame
            self.trace(calling_frame, 'enter', None)

        stack = thread_global.__dict__.setdefault('original_trace_functions', [])
        stack.append(sys.gettrace())
        sys.settrace(self.trace)

    def __exit__(self, exc_type, exc_value, exc_traceback, context=0):
        if not self.config.enabled:
            return

        stack = thread_global.original_trace_functions
        sys.settrace(stack.pop())
        calling_frame = sys._getframe(context + 1)
        self.trace(calling_frame, 'exit', None)
        self.target_frames.discard(calling_frame)
        self.frame_infos.pop(calling_frame, None)

    def _is_internal_frame(self, frame):
        return frame.f_code.co_filename.startswith(internal_directories)

    def trace(self, frame, event, arg):
        if not (frame.f_code in self.target_codes or frame in self.target_frames):
            if (
                    self.depth == 1
                    or self._is_internal_frame(frame)
            ) and not utils.is_comprehension_frame(frame):
                return None
            else:
                candidate = frame
                i = 0
                while True:
                    if utils.is_comprehension_frame(candidate):
                        candidate = candidate.f_back
                        continue
                    i += 1
                    if candidate.f_code in self.target_codes or candidate in self.target_frames:
                        break
                    candidate = candidate.f_back
                    if i >= self.depth or candidate is None or self._is_internal_frame(candidate):
                        return None

        thread_local = self.config.thread_local
        thread_local.__dict__.setdefault('depth', -1)
        frame_info = self.frame_infos.setdefault(frame, FrameInfo(frame))
        if event in ('call', 'enter'):
            thread_local.depth += 1
        elif self.config.last_frame and self.config.last_frame is not frame:
            line_no = self.frame_infos[frame].last_line_no
            trace_event = Event(frame, event, arg, thread_local.depth, line_no=line_no)
            line = self.config.formatter.format_line_only(trace_event)
            self.config.write(line)

        self.config.last_frame = frame

        trace_event = Event(frame, event, arg, thread_local.depth, last_line_no=frame_info.last_line_no)
        if not (frame.f_code.co_name == '<genexpr>' and event not in ('return', 'exception')):
            trace_event.variables = frame_info.update_variables(self.watch, self.watch_extras, event)

        if event in ('return', 'exit'):
            del self.frame_infos[frame]
            thread_local.depth -= 1

        formatted = self.config.formatter.format(trace_event)
        self.config.write(formatted)

        return self.trace

class Spy(object):
    def __init__(self, config):
        self.config = config

    def __call__(self, *args, **kwargs):
        if NO_ASTTOKENS:
            raise Exception("birdseye doesn't support this version of Python")

        try:
            import birdseye
        except ImportError:
            raise Exception("You must install birdseye separately to use spy: pip install birdseye")

        # Decorator without parentheses
        if len(args) == 1 and inspect.isfunction(args[0]) and not kwargs:
            return self._trace(args[0])

        # Decorator with parentheses and perhaps arguments
        def decorator(func):
            return self._trace(func, *args, **kwargs)

        return decorator

    def _trace(self, func, *args, **kwargs):
        # noinspection PyUnresolvedReferences
        from birdseye import eye

        traced = eye(func)
        traced = self.config.snoop(*args, **kwargs)(traced)

        @functools.wraps(func)
        def wrapper(*func_args, **func_kwargs):
            if self.config.enabled:
                final_func = traced
            else:
                final_func = func

            return final_func(*func_args, **func_kwargs)

        return wrapper
        <|endoftext|>


<commit_before># Generate sorting data and store in .txt
# Define the reward function

import torch
from torch.utils.data import Dataset
from torch.autograd import Variable
from tqdm import trange, tqdm
import os
import sys

def reward(sample_solution, USE_CUDA=False):
    """
    The reward for the sorting task is defined as the
    length of the longest sorted consecutive subsequence.

    Input sequences must all be the same length.

    Example:

    input       | output
    ====================
    [1 4 3 5 2] | [5 1 2 3 4]

    The output gets a reward of 4/5, or 0.8

    The range is [1/sourceL, 1]

    Args:
        sample_solution: list of len sourceL of [batch_size]
        Tensors
    Returns:
        [batch_size] containing trajectory rewards
    """
    batch_size = sample_solution[0].size(0)
    sourceL = len(sample_solution)

    longest = Variable(torch.ones(batch_size), requires_grad=False)
    current = Variable(torch.ones(batch_size), requires_grad=False)

    if USE_CUDA:
        longest = longest.cuda()
        current = current.cuda()

    for i in range(1, sourceL):
        # compare solution[i-1] < solution[i]
        res = torch.lt(sample_solution[i-1], sample_solution[i])
        # if res[i,j] == 1, increment length of current sorted subsequence
        current += res.float()
        # else, reset current to 1
        current[torch.eq(res, 0)] = 1
        #current[torch.eq(res, 0)] -= 1
        # if, for any, current > longest, update longest
        mask = torch.gt(current, longest)
        longest[mask] = current[mask]
    return torch.div(longest, sourceL)

def create_dataset(
        train_size,
        val_size,
        #test_size,
        data_dir,
        data_len,
        seed=None):

    if seed is not None:
        torch.manual_seed(seed)

    train_task = 'sorting-size-{}-len-{}-train.txt'.format(train_size, data_len)
    val_task = 'sorting-size-{}-len-{}-val.txt'.format(val_size, data_len)
    #test_task = 'sorting-size-{}-len-{}-test.txt'.format(test_size, data_len)

    train_fname = os.path.join(data_dir, train_task)
    val_fname = os.path.join(data_dir, val_task)

    if not os.path.isdir(data_dir):
        os.mkdir(data_dir)
    else:
        if os.path.exists(train_fname) and os.path.exists(val_fname):
            return train_fname, val_fname

    train_set = open(os.path.join(data_dir, train_task), 'w')
    val_set = open(os.path.join(data_dir, val_task), 'w')
    #test_set = open(os.path.join(data_dir, test_task), 'w')

    def to_string(tensor):
        """
        Convert a a torch.LongTensor
        of size data_len to a string
        of integers separated by whitespace
        and ending in a newline character
        """
        line = ''
        for j in range(data_len-1):
            line += '{} '.format(tensor[j])
        line += str(tensor[-1]) + '\n'
        return line

    print('Creating training data set for {}...'.format(train_task))

    # Generate a training set of size train_size
    for i in trange(train_size):
        x = torch.randperm(data_len)
        train_set.write(to_string(x))

    print('Creating validation data set for {}...'.format(val_task))

    for i in trange(val_size):
        x = torch.randperm(data_len)
        val_set.write(to_string(x))

#    print('Creating test data set for {}...'.format(test_task))
#
#    for i in trange(test_size):
#        x = torch.randperm(data_len)
#        test_set.write(to_string(x))

    train_set.close()
    val_set.close()
#    test_set.close()
    return train_fname, val_fname

class SortingDataset(Dataset):

    def __init__(self, dataset_fname):
        super(SortingDataset, self).__init__()

        self.data_set = []
        with open(dataset_fname, 'r') as dset:
            lines = dset.readlines()
            for next_line in tqdm(lines):
                toks = next_line.split()
                sample = torch.zeros(1, len(toks)).long()
                for idx, tok in enumerate(toks):
                    sample[0, idx] = int(tok)
                self.data_set.append(sample)

        self.size = len(self.data_set)

    def __len__(self):
        return self.size

    def __getitem__(self, idx):
        return self.data_set[idx]

if __name__ == '__main__':
    if int(sys.argv[1]) == 0:
        #sample = Variable(torch.Tensor([[3, 2, 1, 4, 5], [2, 3, 5, 1, 4]]))
        sample = [Variable(torch.Tensor([3,2])), Variable(torch.Tensor([2,3])), Variable(torch.Tensor([1,5])),
                Variable(torch.Tensor([4, 1])), Variable(torch.Tensor([5, 4]))]
        answer = torch.Tensor([3/5., 3/5])

        res = reward(sample)

        print('Expected answer: {}, Actual answer: {}'.format(answer, res.data))
        """
        sample = Variable(torch.Tensor([[1, 2, 3, 4, 5], [5, 4, 3, 2, 1]]))
        answer = torch.Tensor([1., 1/5])

        res = reward(sample)

        print('Expected answer: {}, Actual answer: {}'.format(answer, res.data))

        sample = Variable(torch.Tensor([[1, 2, 5, 4, 3], [4, 1, 2, 3, 5]]))
        answer = torch.Tensor([3/5., 4/5])

        res = reward(sample)

        print('Expected answer: {}, Actual answer: {}'.format(answer, res.data))
        """
    elif int(sys.argv[1]) == 1:
        create_sorting_dataset(1000, 100, 'data', 10, 123)
    elif int(sys.argv[1]) == 2:

        sorting_data = SortingDataset('data', 'sorting-size-1000-len-10-train.txt',
            'sorting-size-100-len-10-val.txt')

        for i in range(len(sorting_data)):
            print(sorting_data[i])
<commit_msg>Fixed sorting<commit_after># Generate sorting data and store in .txt
# Define the reward function

import torch
from torch.utils.data import Dataset
from torch.autograd import Variable
from tqdm import trange, tqdm
import os
import sys

def reward(sample_solution, USE_CUDA=False):
    """
    The reward for the sorting task is defined as the
    length of the longest sorted consecutive subsequence.

    Input sequences must all be the same length.

    Example:

    input       | output
    ====================
    [1 4 3 5 2] | [5 1 2 3 4]

    The output gets a reward of 4/5, or 0.8

    The range is [1/sourceL, 1]

    Args:
        sample_solution: list of len sourceL of [batch_size]
        Tensors
    Returns:
        [batch_size] containing trajectory rewards
    """
    batch_size = sample_solution[0].size(0)
    sourceL = len(sample_solution)

    longest = Variable(torch.ones(batch_size), requires_grad=False)
    current = Variable(torch.ones(batch_size), requires_grad=False)

    if USE_CUDA:
        longest = longest.cuda()
        current = current.cuda()

    for i in range(1, sourceL):
        # compare solution[i-1] < solution[i]
        res = torch.lt(sample_solution[i-1], sample_solution[i])
        # if res[i,j] == 1, increment length of current sorted subsequence
        current += res.float()
        # else, reset current to 1
        current[torch.eq(res, 0)] = 1
        #current[torch.eq(res, 0)] -= 1
        # if, for any, current > longest, update longest
        mask = torch.gt(current, longest)
        longest[mask] = current[mask]
    return -torch.div(longest, sourceL)

def create_dataset(
        train_size,
        val_size,
        #test_size,
        data_dir,
        data_len,
        seed=None):

    if seed is not None:
        torch.manual_seed(seed)

    train_task = 'sorting-size-{}-len-{}-train.txt'.format(train_size, data_len)
    val_task = 'sorting-size-{}-len-{}-val.txt'.format(val_size, data_len)
    #test_task = 'sorting-size-{}-len-{}-test.txt'.format(test_size, data_len)

    train_fname = os.path.join(data_dir, train_task)
    val_fname = os.path.join(data_dir, val_task)

    if not os.path.isdir(data_dir):
        os.mkdir(data_dir)
    else:
        if os.path.exists(train_fname) and os.path.exists(val_fname):
            return train_fname, val_fname

    train_set = open(os.path.join(data_dir, train_task), 'w')
    val_set = open(os.path.join(data_dir, val_task), 'w')
    #test_set = open(os.path.join(data_dir, test_task), 'w')

    def to_string(tensor):
        """
        Convert a a torch.LongTensor
        of size data_len to a string
        of integers separated by whitespace
        and ending in a newline character
        """
        line = ''
        for j in range(data_len-1):
            line += '{} '.format(tensor[j])
        line += str(tensor[-1]) + '\n'
        return line

    print('Creating training data set for {}...'.format(train_task))

    # Generate a training set of size train_size
    for i in trange(train_size):
        x = torch.randperm(data_len)
        train_set.write(to_string(x))

    print('Creating validation data set for {}...'.format(val_task))

    for i in trange(val_size):
        x = torch.randperm(data_len)
        val_set.write(to_string(x))

#    print('Creating test data set for {}...'.format(test_task))
#
#    for i in trange(test_size):
#        x = torch.randperm(data_len)
#        test_set.write(to_string(x))

    train_set.close()
    val_set.close()
#    test_set.close()
    return train_fname, val_fname

class SortingDataset(Dataset):

    def __init__(self, dataset_fname):
        super(SortingDataset, self).__init__()

        print('Loading training data into memory')
        self.data_set = []
        with open(dataset_fname, 'r') as dset:
            lines = dset.readlines()
            for next_line in tqdm(lines):
                toks = next_line.split()
                sample = torch.zeros(1, len(toks)).long()
                for idx, tok in enumerate(toks):
                    sample[0, idx] = int(tok)
                self.data_set.append(sample)

        self.size = len(self.data_set)

    def __len__(self):
        return self.size

    def __getitem__(self, idx):
        return self.data_set[idx]

if __name__ == '__main__':
    if int(sys.argv[1]) == 0:
        #sample = Variable(torch.Tensor([[3, 2, 1, 4, 5], [2, 3, 5, 1, 4]]))
        sample = [Variable(torch.Tensor([3,2])), Variable(torch.Tensor([2,3])), Variable(torch.Tensor([1,5])),
                Variable(torch.Tensor([4, 1])), Variable(torch.Tensor([5, 4]))]
        answer = torch.Tensor([3/5., 3/5])

        res = reward(sample)

        print('Expected answer: {}, Actual answer: {}'.format(answer, res.data))
        """
        sample = Variable(torch.Tensor([[1, 2, 3, 4, 5], [5, 4, 3, 2, 1]]))
        answer = torch.Tensor([1., 1/5])

        res = reward(sample)

        print('Expected answer: {}, Actual answer: {}'.format(answer, res.data))

        sample = Variable(torch.Tensor([[1, 2, 5, 4, 3], [4, 1, 2, 3, 5]]))
        answer = torch.Tensor([3/5., 4/5])

        res = reward(sample)

        print('Expected answer: {}, Actual answer: {}'.format(answer, res.data))
        """
    elif int(sys.argv[1]) == 1:
        create_sorting_dataset(1000, 100, 'data', 10, 123)
    elif int(sys.argv[1]) == 2:

        sorting_data = SortingDataset('data', 'sorting-size-1000-len-10-train.txt',
            'sorting-size-100-len-10-val.txt')

        for i in range(len(sorting_data)):
            print(sorting_data[i])
<|endoftext|>


<commit_before>"""
Helper class for working with the adzuna API
Copied from lm-tools-jobs-api
"""

import requests

from django.conf import settings

class Adzuna(object):
    def __init__(self):
        self.APP_ID = getattr(settings, 'ADZUNA_APP_ID')
        self.APP_KEY = getattr(settings, 'ADZUNA_APP_KEY')
        self.BASE_URL = "http://api.adzuna.com:80/v1/api/"

        assert all((self.APP_ID, self.APP_KEY))

    def _base_request(self, endpoint, params, page=None):
        URL = "{0}{1}".format(self.BASE_URL, endpoint)
        if page:
            URL = "{0}{1}".format(URL, page)

        params.update({
            "app_id": self.APP_ID,
            "app_key": self.APP_KEY,
        })

        req = requests.get(URL, params=params)

        if req.status_code != 200:
            raise ValueError("Got a {0} from Adzuna: {1}".format(
                req.status_code,
                req.json()['display']))
        return req

    def _unwrap_pagination(self, endpoint, params, count):
        num_results = 0
        page = 1

        while num_results <= count:
            results = self._base_request(endpoint, params, page)
            all_results = results.json().get('results', [])

            for result in all_results:
                if num_results < count:
                    num_results += 1
                    yield result
                else:
                    raise StopIteration
            page += 1

    def locations_for_postcode(self, postcode):
        endpoint = "jobs/gb/geodata/"
        params = {
            "where": postcode,
        }
        results = self._base_request(endpoint, params)
        area = results.json()['location']['area']
        assert (len(area) >= 3)
        locations = area[:3]
        return locations

    def jobs_at_location(self, location0, location1, location2, count=10):
        endpoint = "jobs/gb/search/"

        params = {
            "location0": location0,
            "location1": location1,
            "location2": location2,
            "sort_direction": "down",
            "sort_by": "date",
        }

        return self._unwrap_pagination(endpoint, params, count)

    def top_companies(self, location0, location1, location2, count=10):
        endpoint = "jobs/gb/top_companies/"

        params = {
            "location0": location0,
            "location1": location1,
            "location2": location2,
        }
        results = self._base_request(endpoint, params)
        return results.json()['leaderboard'][:count]
<commit_msg>Keep trying shorter postcodes until Adzuna finds a location<commit_after>"""
Helper class for working with the adzuna API
Copied from lm-tools-jobs-api
"""

import requests

from django.conf import settings

class Adzuna(object):
    def __init__(self):
        self.APP_ID = getattr(settings, 'ADZUNA_APP_ID')
        self.APP_KEY = getattr(settings, 'ADZUNA_APP_KEY')
        self.BASE_URL = "http://api.adzuna.com:80/v1/api/"

        assert all((self.APP_ID, self.APP_KEY))

    def _base_request(self, endpoint, params, page=None):
        URL = "{0}{1}".format(self.BASE_URL, endpoint)
        if page:
            URL = "{0}{1}".format(URL, page)

        params.update({
            "app_id": self.APP_ID,
            "app_key": self.APP_KEY,
        })

        req = requests.get(URL, params=params)

        if req.status_code != 200:
            raise ValueError("Got a {0} from Adzuna: {1}".format(
                req.status_code,
                req.json()['display']))
        return req

    def _unwrap_pagination(self, endpoint, params, count):
        num_results = 0
        page = 1

        while num_results <= count:
            results = self._base_request(endpoint, params, page)
            all_results = results.json().get('results', [])

            for result in all_results:
                if num_results < count:
                    num_results += 1
                    yield result
                else:
                    raise StopIteration
            page += 1

    def locations_for_postcode(self, postcode):
        def _get_area(postcode):
            endpoint = "jobs/gb/geodata/"
            params = {
                "where": postcode,
            }
            results = self._base_request(endpoint, params)
            return results.json()['location']['area']
        area = _get_area(postcode)
        # If the postcode isn't found, try a shorter version of it.
        # this is a work around for problems with the Adzuna API.
        while len(area) < 3 and len(postcode) > 0:
            postcode = postcode[:-1]
            area = _get_area(postcode)
        assert (len(area) >= 3)
        locations = area[:3]
        return locations

    def jobs_at_location(self, location0, location1, location2, count=10):
        endpoint = "jobs/gb/search/"

        params = {
            "location0": location0,
            "location1": location1,
            "location2": location2,
            "sort_direction": "down",
            "sort_by": "date",
        }

        return self._unwrap_pagination(endpoint, params, count)

    def top_companies(self, location0, location1, location2, count=10):
        endpoint = "jobs/gb/top_companies/"

        params = {
            "location0": location0,
            "location1": location1,
            "location2": location2,
        }
        results = self._base_request(endpoint, params)
        return results.json()['leaderboard'][:count]
<|endoftext|>


<commit_before>from __future__ import print_function, division

import numpy as np
from numpy.testing import (run_module_suite, assert_array_almost_equal,
                           assert_almost_equal, assert_array_equal,
                           assert_raises)
import warnings

from skimage.restoration import unwrap_phase

def assert_phase_almost_equal(a, b, *args, **kwargs):
    '''An assert_almost_equal insensitive to phase shifts of n*2*pi.'''
    shift = 2 * np.pi * np.round((b.mean() - a.mean()) / (2 * np.pi))
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        print('assert_phase_allclose, abs', np.max(np.abs(a - (b - shift))))
        print('assert_phase_allclose, rel',
              np.max(np.abs((a - (b - shift)) / a)))
    if np.ma.isMaskedArray(a):
        assert np.ma.isMaskedArray(b)
        assert_array_equal(a.mask, b.mask)
        au = np.asarray(a)
        bu = np.asarray(b)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            print('assert_phase_allclose, no mask, abs',
                  np.max(np.abs(au - (bu - shift))))
            print('assert_phase_allclose, no mask, rel',
                  np.max(np.abs((au - (bu - shift)) / au)))
    assert_array_almost_equal(a + shift, b, *args, **kwargs)

def check_unwrap(image, mask=None):
    image_wrapped = np.angle(np.exp(1j * image))
    if not mask is None:
        print('Testing a masked image')
        image = np.ma.array(image, mask=mask)
        image_wrapped = np.ma.array(image_wrapped, mask=mask)
    image_unwrapped = unwrap_phase(image_wrapped)
    assert_phase_almost_equal(image_unwrapped, image)

def test_unwrap_1d():
    image = np.linspace(0, 10 * np.pi, 100)
    check_unwrap(image)
    # Masked arrays are not allowed in 1D
    assert_raises(ValueError, check_unwrap, image, True)
    # wrap_around is not allowed in 1D
    assert_raises(ValueError, unwrap_phase, image, True)

def test_unwrap_2d():
    x, y = np.ogrid[:8, :16]
    image = 2 * np.pi * (x * 0.2 + y * 0.1)
    yield check_unwrap, image
    mask = np.zeros(image.shape, dtype=np.bool)
    mask[4:6, 4:8] = True
    yield check_unwrap, image, mask

def test_unwrap_3d():
    x, y, z = np.ogrid[:8, :12, :16]
    image = 2 * np.pi * (x * 0.2 + y * 0.1 + z * 0.05)
    yield check_unwrap, image
    mask = np.zeros(image.shape, dtype=np.bool)
    mask[4:6, 4:6, 1:3] = True
    yield check_unwrap, image, mask

def check_wrap_around(ndim, axis):
    # create a ramp, but with the last pixel along axis equalling the first
    elements = 100
    ramp = np.linspace(0, 12 * np.pi, elements)
    ramp[-1] = ramp[0]
    image = ramp.reshape(tuple([elements if n == axis else 1
                                for n in range(ndim)]))
    image_wrapped = np.angle(np.exp(1j * image))

    index_first = tuple([0] * ndim)
    index_last = tuple([-1 if n == axis else 0 for n in range(ndim)])
    # unwrap the image without wrap around
    with warnings.catch_warnings():
        # We do not want warnings about length 1 dimensions
        warnings.simplefilter("ignore")
        image_unwrap_no_wrap_around = unwrap_phase(image_wrapped)
    print('endpoints without wrap_around:',
          image_unwrap_no_wrap_around[index_first],
          image_unwrap_no_wrap_around[index_last])
    # without wrap around, the endpoints of the image should differ
    assert abs(image_unwrap_no_wrap_around[index_first]
               - image_unwrap_no_wrap_around[index_last]) > np.pi
    # unwrap the image with wrap around
    wrap_around = [n == axis for n in range(ndim)]
    with warnings.catch_warnings():
        # We do not want warnings about length 1 dimensions
        warnings.simplefilter("ignore")
        image_unwrap_wrap_around = unwrap_phase(image_wrapped, wrap_around)
    print('endpoints with wrap_around:',
          image_unwrap_wrap_around[index_first],
          image_unwrap_wrap_around[index_last])
    # with wrap around, the endpoints of the image should be equal
    assert_almost_equal(image_unwrap_wrap_around[index_first],
                        image_unwrap_wrap_around[index_last])

def test_wrap_around():
    for ndim in (2, 3):
        for axis in range(ndim):
            yield check_wrap_around, ndim, axis

def test_mask():
    length = 100
    ramps = [np.linspace(0, 4 * np.pi, length),
             np.linspace(0, 8 * np.pi, length),
             np.linspace(0, 6 * np.pi, length)]
    image = np.vstack(ramps)
    mask_1d = np.ones((length,), dtype=np.bool)
    mask_1d[0] = mask_1d[-1] = False
    for i in range(len(ramps)):
        # mask all ramps but the i'th one
        mask = np.zeros(image.shape, dtype=np.bool)
        mask |= mask_1d.reshape(1, -1)
        mask[i, :] = False   # unmask i'th ramp
        image_wrapped = np.ma.array(np.angle(np.exp(1j * image)), mask=mask)
        image_unwrapped = unwrap_phase(image_wrapped)
        image_unwrapped -= image_unwrapped[0, 0]    # remove phase shift
        # The end of the unwrapped array should have value equal to the
        # endpoint of the unmasked ramp
        assert_array_almost_equal(image_unwrapped[:, -1], image[i, -1])

        # Same tests, but forcing use of the 3D unwrapper by reshaping
        image_wrapped_3d = image_wrapped.reshape((1,) + image_wrapped.shape)
        image_unwrapped_3d = unwrap_phase(image_wrapped_3d)
        image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0]  # remove phase shift
        assert_array_almost_equal(image_unwrapped_3d[:, :, -1], image[i, -1])

def test_invalid_input():
    assert_raises(ValueError, unwrap_phase, np.zeros([]))
    assert_raises(ValueError, unwrap_phase, np.zeros((1, 1, 1, 1)))
    assert_raises(ValueError, unwrap_phase, np.zeros((1, 1)), 3 * [False])
    assert_raises(ValueError, unwrap_phase, np.zeros((1, 1)), 'False')

if __name__ == "__main__":
    run_module_suite()
<commit_msg>TST: Unit test to check for 3d unwrap seg fault<commit_after>from __future__ import print_function, division

import numpy as np
from numpy.testing import (run_module_suite, assert_array_almost_equal,
                           assert_almost_equal, assert_array_equal,
                           assert_raises)
import warnings

from skimage.restoration import unwrap_phase

def assert_phase_almost_equal(a, b, *args, **kwargs):
    '''An assert_almost_equal insensitive to phase shifts of n*2*pi.'''
    shift = 2 * np.pi * np.round((b.mean() - a.mean()) / (2 * np.pi))
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        print('assert_phase_allclose, abs', np.max(np.abs(a - (b - shift))))
        print('assert_phase_allclose, rel',
              np.max(np.abs((a - (b - shift)) / a)))
    if np.ma.isMaskedArray(a):
        assert np.ma.isMaskedArray(b)
        assert_array_equal(a.mask, b.mask)
        au = np.asarray(a)
        bu = np.asarray(b)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            print('assert_phase_allclose, no mask, abs',
                  np.max(np.abs(au - (bu - shift))))
            print('assert_phase_allclose, no mask, rel',
                  np.max(np.abs((au - (bu - shift)) / au)))
    assert_array_almost_equal(a + shift, b, *args, **kwargs)

def check_unwrap(image, mask=None):
    image_wrapped = np.angle(np.exp(1j * image))
    if not mask is None:
        print('Testing a masked image')
        image = np.ma.array(image, mask=mask)
        image_wrapped = np.ma.array(image_wrapped, mask=mask)
    image_unwrapped = unwrap_phase(image_wrapped)
    assert_phase_almost_equal(image_unwrapped, image)

def test_unwrap_1d():
    image = np.linspace(0, 10 * np.pi, 100)
    check_unwrap(image)
    # Masked arrays are not allowed in 1D
    assert_raises(ValueError, check_unwrap, image, True)
    # wrap_around is not allowed in 1D
    assert_raises(ValueError, unwrap_phase, image, True)

def test_unwrap_2d():
    x, y = np.ogrid[:8, :16]
    image = 2 * np.pi * (x * 0.2 + y * 0.1)
    yield check_unwrap, image
    mask = np.zeros(image.shape, dtype=np.bool)
    mask[4:6, 4:8] = True
    yield check_unwrap, image, mask

def test_unwrap_3d():
    x, y, z = np.ogrid[:8, :12, :16]
    image = 2 * np.pi * (x * 0.2 + y * 0.1 + z * 0.05)
    yield check_unwrap, image
    mask = np.zeros(image.shape, dtype=np.bool)
    mask[4:6, 4:6, 1:3] = True
    yield check_unwrap, image, mask

def check_wrap_around(ndim, axis):
    # create a ramp, but with the last pixel along axis equalling the first
    elements = 100
    ramp = np.linspace(0, 12 * np.pi, elements)
    ramp[-1] = ramp[0]
    image = ramp.reshape(tuple([elements if n == axis else 1
                                for n in range(ndim)]))
    image_wrapped = np.angle(np.exp(1j * image))

    index_first = tuple([0] * ndim)
    index_last = tuple([-1 if n == axis else 0 for n in range(ndim)])
    # unwrap the image without wrap around
    with warnings.catch_warnings():
        # We do not want warnings about length 1 dimensions
        warnings.simplefilter("ignore")
        image_unwrap_no_wrap_around = unwrap_phase(image_wrapped)
    print('endpoints without wrap_around:',
          image_unwrap_no_wrap_around[index_first],
          image_unwrap_no_wrap_around[index_last])
    # without wrap around, the endpoints of the image should differ
    assert abs(image_unwrap_no_wrap_around[index_first]
               - image_unwrap_no_wrap_around[index_last]) > np.pi
    # unwrap the image with wrap around
    wrap_around = [n == axis for n in range(ndim)]
    with warnings.catch_warnings():
        # We do not want warnings about length 1 dimensions
        warnings.simplefilter("ignore")
        image_unwrap_wrap_around = unwrap_phase(image_wrapped, wrap_around)
    print('endpoints with wrap_around:',
          image_unwrap_wrap_around[index_first],
          image_unwrap_wrap_around[index_last])
    # with wrap around, the endpoints of the image should be equal
    assert_almost_equal(image_unwrap_wrap_around[index_first],
                        image_unwrap_wrap_around[index_last])

def test_wrap_around():
    for ndim in (2, 3):
        for axis in range(ndim):
            yield check_wrap_around, ndim, axis

def test_mask():
    length = 100
    ramps = [np.linspace(0, 4 * np.pi, length),
             np.linspace(0, 8 * np.pi, length),
             np.linspace(0, 6 * np.pi, length)]
    image = np.vstack(ramps)
    mask_1d = np.ones((length,), dtype=np.bool)
    mask_1d[0] = mask_1d[-1] = False
    for i in range(len(ramps)):
        # mask all ramps but the i'th one
        mask = np.zeros(image.shape, dtype=np.bool)
        mask |= mask_1d.reshape(1, -1)
        mask[i, :] = False   # unmask i'th ramp
        image_wrapped = np.ma.array(np.angle(np.exp(1j * image)), mask=mask)
        image_unwrapped = unwrap_phase(image_wrapped)
        image_unwrapped -= image_unwrapped[0, 0]    # remove phase shift
        # The end of the unwrapped array should have value equal to the
        # endpoint of the unmasked ramp
        assert_array_almost_equal(image_unwrapped[:, -1], image[i, -1])

        # Same tests, but forcing use of the 3D unwrapper by reshaping
        image_wrapped_3d = image_wrapped.reshape((1,) + image_wrapped.shape)
        image_unwrapped_3d = unwrap_phase(image_wrapped_3d)
        image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0]  # remove phase shift
        assert_array_almost_equal(image_unwrapped_3d[:, :, -1], image[i, -1])

def test_invalid_input():
    assert_raises(ValueError, unwrap_phase, np.zeros([]))
    assert_raises(ValueError, unwrap_phase, np.zeros((1, 1, 1, 1)))
    assert_raises(ValueError, unwrap_phase, np.zeros((1, 1)), 3 * [False])
    assert_raises(ValueError, unwrap_phase, np.zeros((1, 1)), 'False')

def test_unwrap_3d_middle_wrap_around():
    # Segmentation fault in 3D unwrap phase with middle dimension connected
    # GitHub issue #1171
    image = np.zeros((20, 30, 40), dtype=np.float32)
    unwrap = unwrap_phase(image, wrap_around=[False, True, False])
    assert np.all(unwrap == 0)

if __name__ == "__main__":
    run_module_suite()
<|endoftext|>


<commit_before>import numpy as np

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_blobs

from sklearn.utils.class_weight import compute_class_weight
from sklearn.utils.class_weight import compute_sample_weight

from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_raise_message
from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_equal

def test_compute_class_weight():
    # Test (and demo) compute_class_weight.
    y = np.asarray([2, 2, 2, 3, 3, 4])
    classes = np.unique(y)

    cw = compute_class_weight("balanced", classes, y)
    # total effect of samples is preserved
    class_counts = np.bincount(y)[2:]
    assert_almost_equal(np.dot(cw, class_counts), y.shape[0])
    assert_true(cw[0] < cw[1] < cw[2])

def test_compute_class_weight_not_present():
    # Raise error when y does not contain all class labels
    classes = np.arange(4)
    y = np.asarray([0, 0, 0, 1, 1, 2])
    assert_raises(ValueError, compute_class_weight, "balanced", classes, y)
    # Fix exception in error message formatting when missing label is a string
    # https://github.com/scikit-learn/scikit-learn/issues/8312
    assert_raise_message(ValueError,
                         'Class label label_not_present not present',
                         compute_class_weight,
                         {'label_not_present': 1.}, classes, y)
    # Raise error when y has items not in classes
    classes = np.arange(2)
    assert_raises(ValueError, compute_class_weight, "balanced", classes, y)
    assert_raises(ValueError, compute_class_weight, {0: 1., 1: 2.}, classes, y)

def test_compute_class_weight_dict():
    classes = np.arange(3)
    class_weights = {0: 1.0, 1: 2.0, 2: 3.0}
    y = np.asarray([0, 0, 1, 2])
    cw = compute_class_weight(class_weights, classes, y)

    # When the user specifies class weights, compute_class_weights should just
    # return them.
    assert_array_almost_equal(np.asarray([1.0, 2.0, 3.0]), cw)

    # When a class weight is specified that isn't in classes, a ValueError
    # should get raised
    msg = 'Class label 4 not present.'
    class_weights = {0: 1.0, 1: 2.0, 2: 3.0, 4: 1.5}
    assert_raise_message(ValueError, msg, compute_class_weight, class_weights,
                         classes, y)
    msg = 'Class label -1 not present.'
    class_weights = {-1: 5.0, 0: 1.0, 1: 2.0, 2: 3.0}
    assert_raise_message(ValueError, msg, compute_class_weight, class_weights,
                         classes, y)

def test_compute_class_weight_invariance():
    # Test that results with class_weight="balanced" is invariant wrt
    # class imbalance if the number of samples is identical.
    # The test uses a balanced two class dataset with 100 datapoints.
    # It creates three versions, one where class 1 is duplicated
    # resulting in 150 points of class 1 and 50 of class 0,
    # one where there are 50 points in class 1 and 150 in class 0,
    # and one where there are 100 points of each class (this one is balanced
    # again).
    # With balancing class weights, all three should give the same model.
    X, y = make_blobs(centers=2, random_state=0)
    # create dataset where class 1 is duplicated twice
    X_1 = np.vstack([X] + [X[y == 1]] * 2)
    y_1 = np.hstack([y] + [y[y == 1]] * 2)
    # create dataset where class 0 is duplicated twice
    X_0 = np.vstack([X] + [X[y == 0]] * 2)
    y_0 = np.hstack([y] + [y[y == 0]] * 2)
    # duplicate everything
    X_ = np.vstack([X] * 2)
    y_ = np.hstack([y] * 2)
    # results should be identical
    logreg1 = LogisticRegression(class_weight="balanced").fit(X_1, y_1)
    logreg0 = LogisticRegression(class_weight="balanced").fit(X_0, y_0)
    logreg = LogisticRegression(class_weight="balanced").fit(X_, y_)
    assert_array_almost_equal(logreg1.coef_, logreg0.coef_)
    assert_array_almost_equal(logreg.coef_, logreg0.coef_)

def test_compute_class_weight_balanced_negative():
    # Test compute_class_weight when labels are negative
    # Test with balanced class labels.
    classes = np.array([-2, -1, 0])
    y = np.asarray([-1, -1, 0, 0, -2, -2])

    cw = compute_class_weight("balanced", classes, y)
    assert_equal(len(cw), len(classes))
    assert_array_almost_equal(cw, np.array([1., 1., 1.]))

    # Test with unbalanced class labels.
    y = np.asarray([-1, 0, 0, -2, -2, -2])

    cw = compute_class_weight("balanced", classes, y)
    assert_equal(len(cw), len(classes))
    class_counts = np.bincount(y + 2)
    assert_almost_equal(np.dot(cw, class_counts), y.shape[0])
    assert_array_almost_equal(cw, [2. / 3, 2., 1.])

def test_compute_class_weight_balanced_unordered():
    # Test compute_class_weight when classes are unordered
    classes = np.array([1, 0, 3])
    y = np.asarray([1, 0, 0, 3, 3, 3])

    cw = compute_class_weight("balanced", classes, y)
    class_counts = np.bincount(y)[classes]
    assert_almost_equal(np.dot(cw, class_counts), y.shape[0])
    assert_array_almost_equal(cw, [2., 1., 2. / 3])

def test_compute_sample_weight():
    # Test (and demo) compute_sample_weight.
    # Test with balanced classes
    y = np.asarray([1, 1, 1, 2, 2, 2])
    sample_weight = compute_sample_weight("balanced", y)
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with user-defined weights
    sample_weight = compute_sample_weight({1: 2, 2: 1}, y)
    assert_array_almost_equal(sample_weight, [2., 2., 2., 1., 1., 1.])

    # Test with column vector of balanced classes
    y = np.asarray([[1], [1], [1], [2], [2], [2]])
    sample_weight = compute_sample_weight("balanced", y)
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with unbalanced classes
    y = np.asarray([1, 1, 1, 2, 2, 2, 3])
    sample_weight = compute_sample_weight("balanced", y)
    expected_balanced = np.array([0.7777, 0.7777, 0.7777, 0.7777, 0.7777,
                                  0.7777, 2.3333])
    assert_array_almost_equal(sample_weight, expected_balanced, decimal=4)

    # Test with `None` weights
    sample_weight = compute_sample_weight(None, y)
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 1.])

    # Test with multi-output of balanced classes
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]])
    sample_weight = compute_sample_weight("balanced", y)
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with multi-output with user-defined weights
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]])
    sample_weight = compute_sample_weight([{1: 2, 2: 1}, {0: 1, 1: 2}], y)
    assert_array_almost_equal(sample_weight, [2., 2., 2., 2., 2., 2.])

    # Test with multi-output of unbalanced classes
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1], [3, -1]])
    sample_weight = compute_sample_weight("balanced", y)
    assert_array_almost_equal(sample_weight, expected_balanced ** 2, decimal=3)

def test_compute_sample_weight_with_subsample():
    # Test compute_sample_weight with subsamples specified.
    # Test with balanced classes and all samples present
    y = np.asarray([1, 1, 1, 2, 2, 2])
    sample_weight = compute_sample_weight("balanced", y, range(6))
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with column vector of balanced classes and all samples present
    y = np.asarray([[1], [1], [1], [2], [2], [2]])
    sample_weight = compute_sample_weight("balanced", y, range(6))
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with a subsample
    y = np.asarray([1, 1, 1, 2, 2, 2])
    sample_weight = compute_sample_weight("balanced", y, range(4))
    assert_array_almost_equal(sample_weight, [2. / 3, 2. / 3,
                                              2. / 3, 2., 2., 2.])

    # Test with a bootstrap subsample
    y = np.asarray([1, 1, 1, 2, 2, 2])
    sample_weight = compute_sample_weight("balanced", y, [0, 1, 1, 2, 2, 3])
    expected_balanced = np.asarray([0.6, 0.6, 0.6, 3., 3., 3.])
    assert_array_almost_equal(sample_weight, expected_balanced)

    # Test with a bootstrap subsample for multi-output
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]])
    sample_weight = compute_sample_weight("balanced", y, [0, 1, 1, 2, 2, 3])
    assert_array_almost_equal(sample_weight, expected_balanced ** 2)

    # Test with a missing class
    y = np.asarray([1, 1, 1, 2, 2, 2, 3])
    sample_weight = compute_sample_weight("balanced", y, range(6))
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 0.])

    # Test with a missing class for multi-output
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1], [2, 2]])
    sample_weight = compute_sample_weight("balanced", y, range(6))
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 0.])

def test_compute_sample_weight_errors():
    # Test compute_sample_weight raises errors expected.
    # Invalid preset string
    y = np.asarray([1, 1, 1, 2, 2, 2])
    y_ = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]])
    assert_raises(ValueError, compute_sample_weight, "ni", y)
    assert_raises(ValueError, compute_sample_weight, "ni", y, range(4))
    assert_raises(ValueError, compute_sample_weight, "ni", y_)
    assert_raises(ValueError, compute_sample_weight, "ni", y_, range(4))

    # Not "balanced" for subsample
    assert_raises(ValueError,
                  compute_sample_weight, {1: 2, 2: 1}, y, range(4))

    # Not a list or preset for multi-output
    assert_raises(ValueError, compute_sample_weight, {1: 2, 2: 1}, y_)

    # Incorrect length list for multi-output
    assert_raises(ValueError, compute_sample_weight, [{1: 2, 2: 1}], y_)
<commit_msg>TST Added test for the compute_class_weight (#10318)<commit_after>import numpy as np

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_blobs

from sklearn.utils.class_weight import compute_class_weight
from sklearn.utils.class_weight import compute_sample_weight

from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_raise_message
from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_equal

def test_compute_class_weight():
    # Test (and demo) compute_class_weight.
    y = np.asarray([2, 2, 2, 3, 3, 4])
    classes = np.unique(y)

    cw = compute_class_weight("balanced", classes, y)
    # total effect of samples is preserved
    class_counts = np.bincount(y)[2:]
    assert_almost_equal(np.dot(cw, class_counts), y.shape[0])
    assert_true(cw[0] < cw[1] < cw[2])

def test_compute_class_weight_not_present():
    # Raise error when y does not contain all class labels
    classes = np.arange(4)
    y = np.asarray([0, 0, 0, 1, 1, 2])
    assert_raises(ValueError, compute_class_weight, "balanced", classes, y)
    # Fix exception in error message formatting when missing label is a string
    # https://github.com/scikit-learn/scikit-learn/issues/8312
    assert_raise_message(ValueError,
                         'Class label label_not_present not present',
                         compute_class_weight,
                         {'label_not_present': 1.}, classes, y)
    # Raise error when y has items not in classes
    classes = np.arange(2)
    assert_raises(ValueError, compute_class_weight, "balanced", classes, y)
    assert_raises(ValueError, compute_class_weight, {0: 1., 1: 2.}, classes, y)

def test_compute_class_weight_dict():
    classes = np.arange(3)
    class_weights = {0: 1.0, 1: 2.0, 2: 3.0}
    y = np.asarray([0, 0, 1, 2])
    cw = compute_class_weight(class_weights, classes, y)

    # When the user specifies class weights, compute_class_weights should just
    # return them.
    assert_array_almost_equal(np.asarray([1.0, 2.0, 3.0]), cw)

    # When a class weight is specified that isn't in classes, a ValueError
    # should get raised
    msg = 'Class label 4 not present.'
    class_weights = {0: 1.0, 1: 2.0, 2: 3.0, 4: 1.5}
    assert_raise_message(ValueError, msg, compute_class_weight, class_weights,
                         classes, y)
    msg = 'Class label -1 not present.'
    class_weights = {-1: 5.0, 0: 1.0, 1: 2.0, 2: 3.0}
    assert_raise_message(ValueError, msg, compute_class_weight, class_weights,
                         classes, y)

def test_compute_class_weight_invariance():
    # Test that results with class_weight="balanced" is invariant wrt
    # class imbalance if the number of samples is identical.
    # The test uses a balanced two class dataset with 100 datapoints.
    # It creates three versions, one where class 1 is duplicated
    # resulting in 150 points of class 1 and 50 of class 0,
    # one where there are 50 points in class 1 and 150 in class 0,
    # and one where there are 100 points of each class (this one is balanced
    # again).
    # With balancing class weights, all three should give the same model.
    X, y = make_blobs(centers=2, random_state=0)
    # create dataset where class 1 is duplicated twice
    X_1 = np.vstack([X] + [X[y == 1]] * 2)
    y_1 = np.hstack([y] + [y[y == 1]] * 2)
    # create dataset where class 0 is duplicated twice
    X_0 = np.vstack([X] + [X[y == 0]] * 2)
    y_0 = np.hstack([y] + [y[y == 0]] * 2)
    # duplicate everything
    X_ = np.vstack([X] * 2)
    y_ = np.hstack([y] * 2)
    # results should be identical
    logreg1 = LogisticRegression(class_weight="balanced").fit(X_1, y_1)
    logreg0 = LogisticRegression(class_weight="balanced").fit(X_0, y_0)
    logreg = LogisticRegression(class_weight="balanced").fit(X_, y_)
    assert_array_almost_equal(logreg1.coef_, logreg0.coef_)
    assert_array_almost_equal(logreg.coef_, logreg0.coef_)

def test_compute_class_weight_balanced_negative():
    # Test compute_class_weight when labels are negative
    # Test with balanced class labels.
    classes = np.array([-2, -1, 0])
    y = np.asarray([-1, -1, 0, 0, -2, -2])

    cw = compute_class_weight("balanced", classes, y)
    assert_equal(len(cw), len(classes))
    assert_array_almost_equal(cw, np.array([1., 1., 1.]))

    # Test with unbalanced class labels.
    y = np.asarray([-1, 0, 0, -2, -2, -2])

    cw = compute_class_weight("balanced", classes, y)
    assert_equal(len(cw), len(classes))
    class_counts = np.bincount(y + 2)
    assert_almost_equal(np.dot(cw, class_counts), y.shape[0])
    assert_array_almost_equal(cw, [2. / 3, 2., 1.])

def test_compute_class_weight_balanced_unordered():
    # Test compute_class_weight when classes are unordered
    classes = np.array([1, 0, 3])
    y = np.asarray([1, 0, 0, 3, 3, 3])

    cw = compute_class_weight("balanced", classes, y)
    class_counts = np.bincount(y)[classes]
    assert_almost_equal(np.dot(cw, class_counts), y.shape[0])
    assert_array_almost_equal(cw, [2., 1., 2. / 3])

def test_compute_class_weight_default():
    # Test for the case where no weight is given for a present class.
    # Current behaviour is to assign the unweighted classes a weight of 1.
    y = np.asarray([2, 2, 2, 3, 3, 4])
    classes = np.unique(y)
    classes_len = len(classes)

    # Test for non specified weights
    cw = compute_class_weight(None, classes, y)
    assert_equal(len(cw), classes_len)
    assert_array_almost_equal(cw, np.ones(3))

    # Tests for partly specified weights
    cw = compute_class_weight({2: 1.5}, classes, y)
    assert_equal(len(cw), classes_len)
    assert_array_almost_equal(cw, [1.5, 1., 1.])

    cw = compute_class_weight({2: 1.5, 4: 0.5}, classes, y)
    assert_equal(len(cw), classes_len)
    assert_array_almost_equal(cw, [1.5, 1., 0.5])

def test_compute_sample_weight():
    # Test (and demo) compute_sample_weight.
    # Test with balanced classes
    y = np.asarray([1, 1, 1, 2, 2, 2])
    sample_weight = compute_sample_weight("balanced", y)
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with user-defined weights
    sample_weight = compute_sample_weight({1: 2, 2: 1}, y)
    assert_array_almost_equal(sample_weight, [2., 2., 2., 1., 1., 1.])

    # Test with column vector of balanced classes
    y = np.asarray([[1], [1], [1], [2], [2], [2]])
    sample_weight = compute_sample_weight("balanced", y)
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with unbalanced classes
    y = np.asarray([1, 1, 1, 2, 2, 2, 3])
    sample_weight = compute_sample_weight("balanced", y)
    expected_balanced = np.array([0.7777, 0.7777, 0.7777, 0.7777, 0.7777,
                                  0.7777, 2.3333])
    assert_array_almost_equal(sample_weight, expected_balanced, decimal=4)

    # Test with `None` weights
    sample_weight = compute_sample_weight(None, y)
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 1.])

    # Test with multi-output of balanced classes
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]])
    sample_weight = compute_sample_weight("balanced", y)
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with multi-output with user-defined weights
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]])
    sample_weight = compute_sample_weight([{1: 2, 2: 1}, {0: 1, 1: 2}], y)
    assert_array_almost_equal(sample_weight, [2., 2., 2., 2., 2., 2.])

    # Test with multi-output of unbalanced classes
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1], [3, -1]])
    sample_weight = compute_sample_weight("balanced", y)
    assert_array_almost_equal(sample_weight, expected_balanced ** 2, decimal=3)

def test_compute_sample_weight_with_subsample():
    # Test compute_sample_weight with subsamples specified.
    # Test with balanced classes and all samples present
    y = np.asarray([1, 1, 1, 2, 2, 2])
    sample_weight = compute_sample_weight("balanced", y, range(6))
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with column vector of balanced classes and all samples present
    y = np.asarray([[1], [1], [1], [2], [2], [2]])
    sample_weight = compute_sample_weight("balanced", y, range(6))
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.])

    # Test with a subsample
    y = np.asarray([1, 1, 1, 2, 2, 2])
    sample_weight = compute_sample_weight("balanced", y, range(4))
    assert_array_almost_equal(sample_weight, [2. / 3, 2. / 3,
                                              2. / 3, 2., 2., 2.])

    # Test with a bootstrap subsample
    y = np.asarray([1, 1, 1, 2, 2, 2])
    sample_weight = compute_sample_weight("balanced", y, [0, 1, 1, 2, 2, 3])
    expected_balanced = np.asarray([0.6, 0.6, 0.6, 3., 3., 3.])
    assert_array_almost_equal(sample_weight, expected_balanced)

    # Test with a bootstrap subsample for multi-output
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]])
    sample_weight = compute_sample_weight("balanced", y, [0, 1, 1, 2, 2, 3])
    assert_array_almost_equal(sample_weight, expected_balanced ** 2)

    # Test with a missing class
    y = np.asarray([1, 1, 1, 2, 2, 2, 3])
    sample_weight = compute_sample_weight("balanced", y, range(6))
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 0.])

    # Test with a missing class for multi-output
    y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1], [2, 2]])
    sample_weight = compute_sample_weight("balanced", y, range(6))
    assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 0.])

def test_compute_sample_weight_errors():
    # Test compute_sample_weight raises errors expected.
    # Invalid preset string
    y = np.asarray([1, 1, 1, 2, 2, 2])
    y_ = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]])
    assert_raises(ValueError, compute_sample_weight, "ni", y)
    assert_raises(ValueError, compute_sample_weight, "ni", y, range(4))
    assert_raises(ValueError, compute_sample_weight, "ni", y_)
    assert_raises(ValueError, compute_sample_weight, "ni", y_, range(4))

    # Not "balanced" for subsample
    assert_raises(ValueError,
                  compute_sample_weight, {1: 2, 2: 1}, y, range(4))

    # Not a list or preset for multi-output
    assert_raises(ValueError, compute_sample_weight, {1: 2, 2: 1}, y_)

    # Incorrect length list for multi-output
    assert_raises(ValueError, compute_sample_weight, [{1: 2, 2: 1}], y_)
<|endoftext|>


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

from __future__ import unicode_literals
from django.db import DataError
from django.contrib.auth.models import User
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.test import TransactionTestCase
from django.test.utils import override_settings
import factory
from faker import Faker
from splinter import Browser
from .models import CNHProfile

fake = Faker()

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User

    username = fake.username()
    email = fake.email()

# # # # # # # # # # # # #
# Unit Tests for Models #
# # # # # # # # # # # # #

class UserNProfileTests(TransactionTestCase):
    def setUp(self):
        self.user1 = UserFactory()
        self.user1.set_password('abc')
        self.user1.save()
        self.profile1 = self.user1.profile

    def tearDown(self):
        User.objects.all().delete()

    # Test 1
    # Check that a CNHProfile is created when user is created
    def test_create_profile(self):
        self.assertEqual(len(CNHProfile.objects.all()), 1)
        self.assertEqual(self.profile1.user.username, self.user1.username)

    # Test 2
    # Check that profile can accept empty string for nickname
    def test_blank_nickname(self):
        self.profile1.nickname = ''
        self.profile1.save()
        self.assertEqual(self.profile1.nickname, '')

    # Test 3
    # Check that profile can accept empty string for website
    def test_blank_website(self):
        self.profile1.website = ''
        self.profile1.save()
        self.assertEqual(self.profile1.website, '')

    # Test 4
    # Check that profile cannot accept string of length 129 for nickname
    def test_long_nickname(self):
        self.profile1.nickname = 'a' * 129
        with self.assertRaises(DataError):
            self.profile1.save()

    # Test 5
    # Check that profile can accept string of length 129 for website
    def test_long_website(self):
        self.profile1.website = 'a' * 129
        self.profile1.save()
        self.assertEqual(self.profile1.website, 'a' * 129)

    # Test 6
    # Check that is_active property works as expected
    def test_is_active(self):
        self.assertIs(self.user1.is_active, True)
        self.assertIs(self.profile1.is_active, True)

    # Test 7
    # Check that is_active can be changed
    def test_is_not_active(self):
        self.assertIs(self.profile1.is_active, True)
        self.profile1.user.is_active = False
        self.assertIs(self.profile1.is_active, False)

    # Test 8
    # Check that ActiveProfileManager works
    def test_active(self):
        self.assertIs(len(CNHProfile.active.all()), 1)
        self.profile1.user.is_active = False
        self.profile1.user.save()
        self.assertFalse(CNHProfile.active.all())

    # Test 9
    # Check that if user is killed, profile is killed
    def test_no_profile(self):
        self.assertIs(len(CNHProfile.objects.all()), 1)
        self.user1.delete()
        self.assertFalse(CNHProfile.objects.all())

    # Test 10
    # Check that if profile is killed, user is killed
    def test_no_user(self):
        self.assertIs(len(User.objects.all()), 1)
        self.profile1.delete()
        self.assertFalse(User.objects.all())

    # Test 11
    # Check string representation of profile
    def test_string_profile(self):
        self.assertEqual(str(self.profile1), self.user1.username)

# # # # # # # # # # # # #
# Web Tests for Profile #
# # # # # # # # # # # # #

@override_settings(DEBUG=True)
class UnAuthNWebTests(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super(UnAuthNWebTests, cls).setUpClass()
        cls.browser = Browser()

    @classmethod
    def tearDownClass(cls):
        cls.browser.quit()
        super(UnAuthNWebTests, cls).tearDownClass()

    # Test 12
    # Check anonymous get of /profile/
    def test_unauthn_get_profile(self):
        self.browser.visit('%s%s' % (self.live_server_url, '/profile/'))
        self.assertEqual(
            self.browser.url,
            '%s%s' % (self.live_server_url, '/accounts/login/?next=/profile/')
        )

    # Test 13
    # Check anonymous get of /profile/edit/
    def test_unauthn_get_edit(self):
        self.browser.visit('%s%s' % (self.live_server_url, '/profile/edit/'))
        self.assertEqual(
            self.browser.url,
            '%s%s' % (self.live_server_url, '/accounts/login/?next=/profile/edit/')
        )

@override_settings(DEBUG=True)
class AuthNWebTests(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super(AuthNWebTests, cls).setUpClass()
        cls.browser = Browser()

    @classmethod
    def tearDownClass(cls):
        cls.browser.quit()
        super(AuthNWebTests, cls).tearDownClass()

    def setUp(self):
        self.user1 = UserFactory()
        self.user1.set_password('abc')
        self.user1.save()
        self.profile1 = self.user1.profile

        self.browser.visit('%s%s' % (self.live_server_url, '/accounts/login/'))
        self.browser.fill('username', self.user1.username)
        self.browser.fill('password', 'abc')
        self.browser.find_by_value('Log in').first.click()

    def tearDown(self):
        User.objects.all().delete()

    # Test 12
    # Check user get of /profile/
    def test_authn_get_profile(self):
        self.browser.visit('%s%s' % (self.live_server_url, '/profile/'))
        self.assertEqual(
            self.browser.url,
            '%s%s' % (self.live_server_url, '/profile/')
        )

    # Test 13
    # Check user get of /profile/edit/ and post updates
    def test_authn_edit(self):
        self.browser.visit('%s%s' % (self.live_server_url, '/profile/edit/'))
        self.assertEqual(
            self.browser.url,
            '%s%s' % (self.live_server_url, '/profile/edit/')
        )
        # import pdb; pdb.set_trace()
<commit_msg>Remove tests for /profile/edit/ for now<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from django.db import DataError
from django.contrib.auth.models import User
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.test import TransactionTestCase
from django.test.utils import override_settings
import factory
from faker import Faker
from splinter import Browser
from .models import CNHProfile

fake = Faker()

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User

    username = fake.username()
    email = fake.email()

# # # # # # # # # # # # #
# Unit Tests for Models #
# # # # # # # # # # # # #

class UserNProfileTests(TransactionTestCase):
    def setUp(self):
        self.user1 = UserFactory()
        self.user1.set_password('abc')
        self.user1.save()
        self.profile1 = self.user1.profile

    def tearDown(self):
        User.objects.all().delete()

    # Test 1
    # Check that a CNHProfile is created when user is created
    def test_create_profile(self):
        self.assertEqual(len(CNHProfile.objects.all()), 1)
        self.assertEqual(self.profile1.user.username, self.user1.username)

    # Test 2
    # Check that profile can accept empty string for nickname
    def test_blank_nickname(self):
        self.profile1.nickname = ''
        self.profile1.save()
        self.assertEqual(self.profile1.nickname, '')

    # Test 3
    # Check that profile can accept empty string for website
    def test_blank_website(self):
        self.profile1.website = ''
        self.profile1.save()
        self.assertEqual(self.profile1.website, '')

    # Test 4
    # Check that profile cannot accept string of length 129 for nickname
    def test_long_nickname(self):
        self.profile1.nickname = 'a' * 129
        with self.assertRaises(DataError):
            self.profile1.save()

    # Test 5
    # Check that profile can accept string of length 129 for website
    def test_long_website(self):
        self.profile1.website = 'a' * 129
        self.profile1.save()
        self.assertEqual(self.profile1.website, 'a' * 129)

    # Test 6
    # Check that is_active property works as expected
    def test_is_active(self):
        self.assertIs(self.user1.is_active, True)
        self.assertIs(self.profile1.is_active, True)

    # Test 7
    # Check that is_active can be changed
    def test_is_not_active(self):
        self.assertIs(self.profile1.is_active, True)
        self.profile1.user.is_active = False
        self.assertIs(self.profile1.is_active, False)

    # Test 8
    # Check that ActiveProfileManager works
    def test_active(self):
        self.assertIs(len(CNHProfile.active.all()), 1)
        self.profile1.user.is_active = False
        self.profile1.user.save()
        self.assertFalse(CNHProfile.active.all())

    # Test 9
    # Check that if user is killed, profile is killed
    def test_no_profile(self):
        self.assertIs(len(CNHProfile.objects.all()), 1)
        self.user1.delete()
        self.assertFalse(CNHProfile.objects.all())

    # Test 10
    # Check that if profile is killed, user is killed
    def test_no_user(self):
        self.assertIs(len(User.objects.all()), 1)
        self.profile1.delete()
        self.assertFalse(User.objects.all())

    # Test 11
    # Check string representation of profile
    def test_string_profile(self):
        self.assertEqual(str(self.profile1), self.user1.username)

# # # # # # # # # # # # #
# Web Tests for Profile #
# # # # # # # # # # # # #

@override_settings(DEBUG=True)
class UnAuthNWebTests(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super(UnAuthNWebTests, cls).setUpClass()
        cls.browser = Browser()

    @classmethod
    def tearDownClass(cls):
        cls.browser.quit()
        super(UnAuthNWebTests, cls).tearDownClass()

    # Test 12
    # Check anonymous get of /profile/
    def test_unauthn_get_profile(self):
        self.browser.visit('%s%s' % (self.live_server_url, '/profile/'))
        self.assertEqual(
            self.browser.url,
            '%s%s' % (self.live_server_url, '/accounts/login/?next=/profile/')
        )

    # Test 13 - future consideration
    # Check anonymous get of /profile/edit/
    # def test_unauthn_get_edit(self):
    #     self.browser.visit('%s%s' % (self.live_server_url, '/profile/edit/'))
    #     self.assertEqual(
    #         self.browser.url,
    #         '%s%s' % (self.live_server_url, '/accounts/login/?next=/profile/edit/')
    #     )

@override_settings(DEBUG=True)
class AuthNWebTests(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super(AuthNWebTests, cls).setUpClass()
        cls.browser = Browser()

    @classmethod
    def tearDownClass(cls):
        cls.browser.quit()
        super(AuthNWebTests, cls).tearDownClass()

    def setUp(self):
        self.user1 = UserFactory()
        self.user1.set_password('abc')
        self.user1.save()
        self.profile1 = self.user1.profile

        self.browser.visit('%s%s' % (self.live_server_url, '/accounts/login/'))
        self.browser.fill('username', self.user1.username)
        self.browser.fill('password', 'abc')
        self.browser.find_by_value('Log in').first.click()

    def tearDown(self):
        User.objects.all().delete()

    # Test 12
    # Check user get of /profile/
    def test_authn_get_profile(self):
        self.browser.visit('%s%s' % (self.live_server_url, '/profile/'))
        self.assertEqual(
            self.browser.url,
            '%s%s' % (self.live_server_url, '/profile/')
        )

    # Test 13
    # Check user get of /profile/edit/ and post updates - future consideration
    # def test_authn_edit(self):
    #     self.browser.visit('%s%s' % (self.live_server_url, '/profile/edit/'))
    #     self.assertEqual(
    #         self.browser.url,
    #         '%s%s' % (self.live_server_url, '/profile/edit/')
    #     )
<|endoftext|>


<commit_before>from __future__ import division
from django.views.generic import TemplateView
from .models import CNHProfile

class ProfileView(TemplateView):
    template_name = 'profile.html'

    def get_context_data(self, **kwargs):
        context = super(ProfileView, self).get_context_data(**kwargs)
        try:
            context['user'] = self.request.user
            scores = self.request.user.scores.all()
            avg_gwpm = 0
            avg_nwpm = 0
            avg_mistakes = 0
            count = 0
            for i in range(len(scores)-1):
                avg_gwpm += scores[i].wpm_gross
                avg_nwpm += scores[i].wpm_net
                avg_mistakes += scores[i].mistakes
                count += 1
            context['avg_gwpm'] = avg_gwpm/count
            context['avg_nwpm'] = avg_nwpm/count
            context['avg_mistakes'] = avg_mistakes/count
        except ZeroDivisionError, CNHProfile.DoesNotExist:
            pass
        return context
<commit_msg>Fix 'except' bug in cnh_profile/views<commit_after>from __future__ import division
from django.views.generic import TemplateView
from .models import CNHProfile

class ProfileView(TemplateView):
    template_name = 'profile.html'

    def get_context_data(self, **kwargs):
        context = super(ProfileView, self).get_context_data(**kwargs)
        try:
            context['user'] = self.request.user
            scores = self.request.user.scores.all()
            avg_gwpm = 0
            avg_nwpm = 0
            avg_mistakes = 0
            count = 0
            for i in range(len(scores)-1):
                avg_gwpm += scores[i].wpm_gross
                avg_nwpm += scores[i].wpm_net
                avg_mistakes += scores[i].mistakes
                count += 1
            context['avg_gwpm'] = avg_gwpm/count
            context['avg_nwpm'] = avg_nwpm/count
            context['avg_mistakes'] = avg_mistakes/count
        except (ZeroDivisionError, CNHProfile.DoesNotExist):
            pass
        return context
<|endoftext|>


<commit_before>from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
import redis
import urllib

with open("typing_test/test1.txt", "r") as myfile:
    data2 = myfile.read().replace('\n', '\\n')
    data2 = data2.replace('    ', '\t')

def play_view(request):
    return render(request, 'typingtest2.html', {'data2': data2})

@csrf_exempt
def multi_play_view(request):
    if request.method == 'POST':
        r = redis.StrictRedis(host='localhost', port=6379, db=0)
        r.set(request.user.username, request.POST['user_input'])
        opponent_data = r.get(request.POST['opponent'])
        return render(request, 'typingtest3.html',
                      {'opponent_data': opponent_data, 'data2': data2})
    else:
        return render(request, 'typingtest3.html', {'data2': data2})

@csrf_exempt
def matchmaking_view(request):
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    opponent = ''
    if r.get('host') is None:
        r.set('host', request.user.username)
        while r.get('guest') is None:
            pass
        opponent = r.get('guest')
    else:
        r.set('guest', request.user.username)
        opponent = r.get('host')
    return render(request, 'typingtest3.html', {'opponent': opponent,
                                                'data2': data2})

@csrf_exempt
def get_content_view(request):
    user = request.POST['user']
    repo = request.POST['repo']
    path = request.POST['path']
    print user, repo, path
    code = urllib.urlopen("https://raw.githubusercontent.com/{}/{}/master/{}"
                          .format(user, repo, path)).read()
    code = str(code)
    return HttpResponse(code)
<commit_msg>deleted keys for matchmaking when done<commit_after>from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
import redis
import urllib

with open("typing_test/test1.txt", "r") as myfile:
    data2 = myfile.read().replace('\n', '\\n')
    data2 = data2.replace('    ', '\t')

def play_view(request):
    return render(request, 'typingtest2.html', {'data2': data2})

@csrf_exempt
def multi_play_view(request):
    if request.method == 'POST':
        r = redis.StrictRedis(host='localhost', port=6379, db=0)
        r.set(request.user.username, request.POST['user_input'])
        opponent_data = r.get(request.POST['opponent'])
        return render(request, 'typingtest3.html',
                      {'opponent_data': opponent_data, 'data2': data2})
    else:
        return render(request, 'typingtest3.html', {'data2': data2})

@csrf_exempt
def matchmaking_view(request):
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    opponent = ''
    if r.get('host') is None:
        r.set('host', request.user.username)
        while r.get('guest') is None:
            pass
        opponent = r.get('guest')
        r.delete('guest')
    else:
        r.set('guest', request.user.username)
        opponent = r.get('host')
        r.delete('host')
    return render(request, 'typingtest3.html', {'opponent': opponent,
                                                'data2': data2})

@csrf_exempt
def get_content_view(request):
    user = request.POST['user']
    repo = request.POST['repo']
    path = request.POST['path']
    print user, repo, path
    code = urllib.urlopen("https://raw.githubusercontent.com/{}/{}/master/{}"
                          .format(user, repo, path)).read()
    code = str(code)
    return HttpResponse(code)
<|endoftext|>