Exemple #1
0
def test_update_form(app_fixture, model_fixtures):
    test_client = app_fixture.test_client()

    response = json_call(test_client.get, '/forms/beta-feedback')
    assert response.status_code == 200

    form = response.json
    form['description'] = 'Form used to get feedback on beta.phila.gov'

    response = json_call(test_client.put, '/forms/beta-feedback', form)
    assert response.status_code == 200
    assert dict_contains(
        response.json, {
            'name': 'beta-feedback',
            'title': 'Beta Feedback Form',
            'description': 'Form used to get feedback on beta.phila.gov',
            'action': 'webhook',
            'forms': [],
            'schema': None,
            'action_config': {
                'url': 'https://example.com/webhook'
            },
            'max_submissions_per_minute': 5,
            'created_at': iso_regex,
            'updated_at': iso_regex
        })
Exemple #2
0
def test_create_form(app_fixture, model_fixtures):
    test_client = app_fixture.test_client()

    response = json_call(
        test_client.post, '/forms', {
            'name': 'my-form',
            'title': 'My Form',
            'action': 'webhook',
            'action_config': {
                'url': 'https://foo.com/webhook'
            }
        })
    assert response.status_code == 201
    assert dict_contains(
        response.json, {
            'name': 'my-form',
            'title': 'My Form',
            'description': None,
            'action': 'webhook',
            'forms': [],
            'schema': None,
            'action_config': {
                'url': 'https://foo.com/webhook'
            },
            'max_submissions_per_minute': 5,
            'created_at': iso_regex,
            'updated_at': iso_regex
        })
Exemple #3
0
 def loss_variance(cls, result, config=None):
     if config['dataset_name'] not in _dataset_cache:
         _dataset_cache[config['dataset_name']] = json_call(
             config['dataset_name'])
     dataset = _dataset_cache[config['dataset_name']]
     n_valid = dataset.descr['n_valid']
     p = cls.loss(result, config)
     if p is None:
         return None
     return p * (1.0 - p) / (n_valid - 1)
 def loss_variance(cls, result, config=None):
     if config['dataset_name'] not in _dataset_cache:
         _dataset_cache[config['dataset_name']] = json_call(
             config['dataset_name'])
     dataset = _dataset_cache[config['dataset_name']]
     n_valid = dataset.descr['n_valid']
     p = cls.loss(result, config)
     if p is None:
         return None
     return p * (1.0 - p) / (n_valid - 1)
def test_submit_form(app_fixture, model_fixtures, monkeypatch):
    mock_google_response = MockGoogleResponse()
    def mock_google_recaptcha(data):
        return mock_google_response
    monkeypatch.setattr(resources, 'google_recaptcha', mock_google_recaptcha)
    test_client = app_fixture.test_client()

    response = json_call(test_client.post, '/forms/beta-feedback/submissions', {
        'recaptcha': 'somerandomslug',
        'what_happened': 'I have a suggestion or idea.',
        'tell_us_more': 'Have more pictures of puppies.'
    })
    assert response.status_code == 204

    response = json_call(test_client.get, '/submissions?form_name=beta-feedback')
    assert response.status_code == 200
    assert response.json['count'] == 1
    assert response.json['page'] == 1
    assert response.json['total_pages'] == 1
    assert len(response.json['data']) == 1
    assert dict_contains(response.json['data'][0], {
        'id': 1,
        'form': 'beta-feedback', ## TODO: form vs form_name ?
        'status': 'submitted',
        'data': {
            'tell_us_more': 'Have more pictures of puppies.',
            'what_happened': 'I have a suggestion or idea.'
        },
        'referrer': None,
        'user_agent': 'werkzeug/0.12.2',
        'ip': '127.0.0.1',
        'worker_id': None,
        'locked_at': None,
        'retry_delay': 300,
        'timeout': 600,
        'attempts': 0,
        'max_attempts': 1,
        'created_at': iso_regex,
        'updated_at': iso_regex
    })
Exemple #6
0
def test_list_forms(app_fixture, model_fixtures):
    test_client = app_fixture.test_client()

    response = json_call(test_client.get, '/forms')
    assert response.status_code == 200
    assert response.json['count'] == 2
    assert response.json['page'] == 1
    assert response.json['total_pages'] == 1
    assert len(response.json['data']) == 2
    assert dict_contains(
        response.json['data'][0], {
            'name': 'beta-feedback',
            'title': 'Beta Feedback Form',
            'description': None,
            'action': 'webhook',
            'forms': [],
            'schema': None,
            'action_config': {
                'url': 'https://example.com/webhook'
            },
            'max_submissions_per_minute': 5,
            'created_at': iso_regex,
            'updated_at': iso_regex
        })
    assert dict_contains(
        response.json['data'][1], {
            'name': 'parks-contact-us',
            'title': 'Parks and Recreation Contact Us Form',
            'description':
            'Primary ontact us form for the Parks and Recreation Department',
            'action': 'store_only',
            'forms': [],
            'schema': None,
            'action_config': None,
            'max_submissions_per_minute': 5,
            'created_at': iso_regex,
            'updated_at': iso_regex
        })
Exemple #7
0
def preprocess_data(config, ctrl):
    dataset = json_call(config['dataset_name'])
    train, valid, test = classification_train_valid_test(dataset)
    X_train, y_train = numpy.asarray(train[0]), numpy.asarray(train[1])
    X_valid, y_valid = numpy.asarray(valid[0]), numpy.asarray(valid[1])
    X_test, y_test = numpy.asarray(test[0]), numpy.asarray(test[1])

    if config['preprocessing']['kind'] == 'pca':
        # compute pca of input (TODO: retrieve only pca_whitened input)
        raise NotImplementedError('rewrite since cut and paste')
        (eigvals,eigvecs), centered_trainset = pylearn_pca.pca_from_examples(
                X=dataset['inputs'][:dataset['n_train']],
                max_energy_fraction=config['pca_energy'])
        eigmean = dataset['inputs'][0] - centered_trainset[0]

        whitened_inputs = pylearn_pca.pca_whiten((eigvals,eigvecs),
                dataset['inputs']-eigmean)
        ctrl.info('PCA kept %i of %i components'%(whitened_inputs.shape[1],
            dataset['n_inputs']))
    elif config['preprocessing']['kind'] == 'zca':
        (eigvals,eigvecs), centered_trainset = pylearn_pca.pca_from_examples(
                X=X_train,
                max_energy_fraction=config['preprocessing']['energy'])
        eigmean = X_train[0] - centered_trainset[0]

        def whiten(X):
            X = pylearn_pca.pca_whiten((eigvals,eigvecs),
                    X - eigmean)
            X = pylearn_pca.pca_whiten_inverse((eigvals, eigvecs),
                    X) + eigmean
            X = X.astype('float32')
            X_min = X.min()
            X_max = X.max()
            ctrl.info('ZCA min:%f max:%f' % (X_min, X_max))
            if X_min < 0 or X_max > 1.0:
                ctrl.info('ZCA clamping return value to (0, 1) interval')
                X = numpy.clip(X, 0, 1, out=X)
            return X

        X_train, X_valid, X_test = [whiten(X)
                for X in [X_train, X_valid, X_test]]

    elif config['preprocessing']['kind'] == 'normalize':
        raise NotImplementedError('rewrite since cut and paste')
        n_train=dataset['n_train']
        whitened_inputs = dataset['inputs']
        whitened_inputs = whitened_inputs - whitened_inputs[:n_train].mean(axis=0)
        whitened_inputs /= whitened_inputs[:n_train].std(axis=0)+1e-7
    elif config['preprocessing']['kind'] == 'raw':
        pass
    else:
        raise ValueError(
                'unrecognized preprocessing',
                config['preprocessing']['kind'])

    for Xy in 'X', 'y':
        for suffix in 'train', 'valid', 'test':
            varname = '%s_%s'%(Xy, suffix)
            var = locals()[varname]
            ctrl.info('%s shape=%s max=%f min=%f' % (
                varname,
                var.shape,
                var.max(),
                var.min()))

    s_X_train = theano.shared(X_train)
    s_y_train = theano.shared(y_train)
    s_X_valid = theano.shared(X_valid)
    s_y_valid = theano.shared(y_valid)
    s_X_test = theano.shared(X_test)
    s_y_test = theano.shared(y_test)

    return (dataset,
            (s_X_train, s_y_train),
            (s_X_valid, s_y_valid),
            (s_X_test, s_y_test))
def preprocess_data(config, ctrl):
    dataset = json_call(config['dataset_name'])
    train, valid, test = classification_train_valid_test(dataset)
    X_train, y_train = numpy.asarray(train[0]), numpy.asarray(train[1])
    X_valid, y_valid = numpy.asarray(valid[0]), numpy.asarray(valid[1])
    X_test, y_test = numpy.asarray(test[0]), numpy.asarray(test[1])

    if config['preprocessing']['kind'] == 'pca':
        # compute pca of input (TODO: retrieve only pca_whitened input)
        raise NotImplementedError('rewrite since cut and paste')
        (eigvals, eigvecs), centered_trainset = pylearn_pca.pca_from_examples(
            X=dataset['inputs'][:dataset['n_train']],
            max_energy_fraction=config['pca_energy'])
        eigmean = dataset['inputs'][0] - centered_trainset[0]

        whitened_inputs = pylearn_pca.pca_whiten((eigvals, eigvecs),
                                                 dataset['inputs'] - eigmean)
        ctrl.info('PCA kept %i of %i components' %
                  (whitened_inputs.shape[1], dataset['n_inputs']))
    elif config['preprocessing']['kind'] == 'zca':
        (eigvals, eigvecs), centered_trainset = pylearn_pca.pca_from_examples(
            X=X_train, max_energy_fraction=config['preprocessing']['energy'])
        eigmean = X_train[0] - centered_trainset[0]

        def whiten(X):
            X = pylearn_pca.pca_whiten((eigvals, eigvecs), X - eigmean)
            X = pylearn_pca.pca_whiten_inverse((eigvals, eigvecs), X) + eigmean
            X = X.astype('float32')
            X_min = X.min()
            X_max = X.max()
            ctrl.info('ZCA min:%f max:%f' % (X_min, X_max))
            if X_min < 0 or X_max > 1.0:
                ctrl.info('ZCA clamping return value to (0, 1) interval')
                X = numpy.clip(X, 0, 1, out=X)
            return X

        X_train, X_valid, X_test = [
            whiten(X) for X in [X_train, X_valid, X_test]
        ]

    elif config['preprocessing']['kind'] == 'normalize':
        raise NotImplementedError('rewrite since cut and paste')
        n_train = dataset['n_train']
        whitened_inputs = dataset['inputs']
        whitened_inputs = whitened_inputs - whitened_inputs[:n_train].mean(
            axis=0)
        whitened_inputs /= whitened_inputs[:n_train].std(axis=0) + 1e-7
    elif config['preprocessing']['kind'] == 'raw':
        pass
    else:
        raise ValueError('unrecognized preprocessing',
                         config['preprocessing']['kind'])

    for Xy in 'X', 'y':
        for suffix in 'train', 'valid', 'test':
            varname = '%s_%s' % (Xy, suffix)
            var = locals()[varname]
            ctrl.info('%s shape=%s max=%f min=%f' %
                      (varname, var.shape, var.max(), var.min()))

    s_X_train = theano.shared(X_train)
    s_y_train = theano.shared(y_train)
    s_X_valid = theano.shared(X_valid)
    s_y_valid = theano.shared(y_valid)
    s_X_test = theano.shared(X_test)
    s_y_test = theano.shared(y_test)

    return (dataset, (s_X_train, s_y_train), (s_X_valid, s_y_valid),
            (s_X_test, s_y_test))