def test_dimensions(set_schema): tmin = Variable(tree=[ 'select', [ ['source', [ {'type': 'Table', 'id': 30, 'name': 'cnty_24k97_data', 'field': 'fid'}, ]], 'tmin' ] ]) assert tmin.dimensions == 'time' counties = Variable(tree=[ 'select', [ ['source', [ {'type': 'Layer', 'id': 26, 'name': 'cnty_24k97', 'field': 'fid'}, ]], 'name' ] ]) assert counties.dimensions == 'space' tmin = Variable(tree=[ 'select', [ ['join', [ {'type': 'Layer', 'id': 26, 'name': 'cnty_24k97', 'field': 'fid'}, {'type': 'Table', 'id': 30, 'name': 'cnty_24k97_data', 'field': 'fid'}, ]], 'tmin' ] ]) assert tmin.dimensions == 'spacetime'
def test_named_nodes(): v = Variable(tree=[ 'named', [ 'TestName', [ 'mean', [ _matrix_val({ 'values': np.array([[1, 2], [3, 4]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }), _matrix_val({ 'values': np.array([[5, 6], [7, 8]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }) ] ] ] ]) np.testing.assert_array_equal(v.data().values, np.array([[3, 4], [5, 6]])) assert v.root.name() == 'TestName'
def test_abscent_source(set_schema): v = Variable(tree=[ 'join', [ {'type': 'Layer', 'id': -1, 'name': 'cnty_24k97', 'field': 'fid'}, {'type': 'Table', 'id': -1, 'name': 'cnty_24k97_data', 'field': 'fid'}, ] ]) with pytest.raises(ObjectDoesNotExist): v.data()
def test_join_two_tables(set_schema): tmin = Variable(tree=[ 'select', [ ['join', [ {'type': 'Table', 'id': 30, 'name': 'cnty_24k97_data', 'field': 'fid'}, {'type': 'Table', 'id': 30, 'name': 'cnty_24k97_data', 'field': 'fid'}, ]], 'tmin' ] ]) assert len(tmin.data()) == 3650
def test_get_layers(set_schema): tmin = Variable(tree=[ 'select', [ ['join', [ {'type': 'Layer', 'id': 26, 'name': 'cnty_24k97', 'field': 'fid'}, {'type': 'Table', 'id': 30, 'name': 'cnty_24k97_data', 'field': 'fid'}, ]], 'tmin' ] ]) assert tmin.get_layers() == set([26])
def test_temporal_mean_operator(): v = Variable(tree=[ 'tmean', [ _matrix_val({ 'values': np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), 'spatial_key': spatial_key3, 'temporal_key': temporal_key3 }) ] ]) np.testing.assert_array_equal(v.data().values, np.array([2, 5, 8]))
def test_raster(set_schema): tree = ["select", [ ["raster", [ { "id": "modis_indices_ndvi", "name": "Land indices"}, [ "source", [ { "field": "fid", "type": "Layer", "id": "26", "name": "cnty_24k97"} ]], "2015-001,2015-030" ]], {"field": "mean", "name": "raster"} ]] mean = Variable(tree=tree) len(mean.get_layers()) == 1 len(mean.get_rasters()) == 1
def submit(request): if not request.is_ajax() or not request.method == 'POST': return HttpResponse('invalid request to /submit') elif not 'jtv_verified' in request.session or request.session['jtv_verified'] != True: return HttpResponse(json.dumps({ 'success': False, 'message': 'You have not verified your JTV account! Please do that before signing up for monobattles!' })) elif not Variable.get_bool('monobattles_enabled'): return HttpResponse(json.dumps({ 'success': False, 'message': 'Monobattles are currently disabled' })) elif not request.POST['sc2_name'] or not request.POST['sc2_charcode'] or not request.POST['sc2_charcode'].isdigit(): return HttpResponse(json.dumps({ 'success': False, 'message': 'Please fill out your sc2 character name and code' })) submission = ViewerSubmission( sc2_name=request.POST['sc2_name'], sc2_charcode=request.POST['sc2_charcode'], submit_time=time() ) submission.save() return HttpResponse(json.dumps({ 'success': True, 'message': 'You signed up for monobattles!' }))
def manage_action(request): if not request.is_ajax() or not request.method == 'POST': return HttpResponse('invalid request to /manage-action') elif not request.user.is_authenticated() or not request.user.has_perm('variables.add_variable'): return HttpResponse(json.dumps({ 'success': False, 'message': 'You are not allowed to enable/disable monobattles' })) if request.POST['action'] == 'enable': Variable.set('monobattles_enabled', True) Variable.set('monobattles_last_enabled_time', time()) return HttpResponse(json.dumps({ 'success': True, 'message': 'Monobattles are ON!' })) Variable.set('monobattles_enabled', False) last_enabled = Variable.get_decimal('monobattles_last_enabled_time', time()) submissions_query = ViewerSubmission.objects.filter( submit_time__gte=last_enabled ).order_by('submit_time') submissions = [] for s in submissions_query: submissions.append({ 'name': s.sc2_name, 'code': s.sc2_charcode, 'time': s.__unicode__() }) return HttpResponse(json.dumps({ 'success': True, 'message': 'Monobattles have been disabled', 'submissions': submissions }))
def test_mean_operator(): v = Variable(tree=[ 'mean', [ _matrix_val({ 'values': np.array([[1, 2], [3, 4]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }), _matrix_val({ 'values': np.array([[5, 6], [7, 8]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }) ] ]) np.testing.assert_array_equal(v.data().values, np.array([[3, 4], [5, 6]])) with pytest.raises(ValueError): v = Variable(tree=[ 'mean', [ _matrix_val({ 'values': np.array([[1, 2], [3, 4]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }), _matrix_val({ 'values': np.array([[1], [2]]), 'spatial_key': [1], 'temporal_key': [date(2010, 1, 1)] }), ] ]) v.data()
def test_example_0_5_select(set_schema): tmin = [ 'select', [ ['join', [ {'type': 'Layer', 'id': 26, 'name': 'cnty_24k97', 'field': 'fid'}, {'type': 'Table', 'id': 30, 'name': 'cnty_24k97_data', 'field': 'fid'}, ]], 'tmin' ] ] tmax = [ 'select', [ ['join', [ {'type': 'Layer', 'id': 26, 'name': 'cnty_24k97', 'field': 'fid'}, {'type': 'Table', 'id': 30, 'name': 'cnty_24k97_data', 'field': 'fid'}, ]], 'tmax' ] ] t_summer = ['tfilter', [ ['mean', [ tmin, tmax ]], { 'filter_type': 'inclusive', 'date_ranges': [ {'start': '2010-06-01', 'end': '2010-08-31'}, {'start': '2011-06-01', 'end': '2011-08-31'}, {'start': '2012-06-01', 'end': '2012-08-31'}, {'start': '2013-06-01', 'end': '2013-08-31'}, {'start': '2014-06-01', 'end': '2014-08-31'} ] } ]] t_norm_summer = Variable(tree=['tmean', [t_summer]]) print t_norm_summer.data() assert np.allclose( t_norm_summer.data().values, [ 14.188587, 22.986413 ] )
def test_temporal_filter_operator(): twobyeight = _matrix_val({ 'values': np.array([[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8]]), 'spatial_key': [1, 2], 'temporal_key': [ DateRange(date(2010, 1, 1), date(2010, 1, 2)), DateRange(date(2010, 1, 2), date(2010, 1, 3)), DateRange(date(2010, 1, 3), date(2010, 1, 4)), DateRange(date(2010, 1, 4), date(2010, 1, 5)), DateRange(date(2010, 1, 5), date(2010, 1, 6)), DateRange(date(2010, 1, 6), date(2010, 1, 7)), DateRange(date(2010, 1, 7), date(2010, 1, 8)), DateRange(date(2010, 1, 8), date(2010, 1, 9)), ] }) v = Variable(tree=[ 'tfilter', [ twobyeight, { 'filter_type': 'inclusive', 'date_ranges': [ { 'start': '2010-01-02', 'end': '2010-01-04' }, { 'start': '2010-01-06', 'end': '2010-01-08' }, ] } ] ]) np.testing.assert_array_equal(v.data().values, [[2, 3, 4, 6, 7, 8], [2, 3, 4, 6, 7, 8]]) v = Variable(tree=[ 'tfilter', [ twobyeight, { 'filter_type': 'exclusive', 'date_ranges': [ { 'start': '2010-01-02', 'end': '2010-01-04' }, { 'start': '2010-01-06', 'end': '2010-01-08' }, ] } ] ]) np.testing.assert_array_equal(v.data().values, [[1, 5], [1, 5]])
def index(request): params = {} if 'jtv_verified' in request.session and request.session['jtv_verified'] == True: params['jtv_verified'] = True else: params['jtv_verified'] = False params['verify_form'] = VerifyForm() params['submit_form'] = SubmitForm() params['monobattles_enabled'] = Variable.get_bool('monobattles_enabled'); return render_to_response('viewers/index.html', params, context_instance=RequestContext(request) )
def manage(request): params = {} if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: login(request, user) return redirect('/manage') else: params['error'] = "Invalid username or password" if request.user.is_authenticated() and request.user.has_perm('variables.add_variable'): template = 'viewers/manage.html' params['is_enabled'] = Variable.get_bool('monobattles_enabled') else: params['login_form'] = AuthenticationForm() template = 'viewers/manage_login.html' return render_to_response(template, params, context_instance=RequestContext(request) )
def test_spatial_filter(): f1 = Feature() f2 = Feature() f1.geometry = GEOSGeometry( 'GEOMETRYCOLLECTION(POLYGON(( 12 12, 12 18, 18 18, 18 17, 12 12)))') f2.geometry = GEOSGeometry( 'GEOMETRYCOLLECTION(POLYGON(( 1 1, 1 2, 2 2, 2 1, 1 1)))') v = Variable(tree=[ 'sfilter', [{ 'values': np.array([[1, 2, 3, 4], [5, 6, 7, 8]]), 'spatial_key': [f1, f2], 'temporal_key': [] }, { 'filter_type': 'inclusive', 'containing_geometries': [ GEOSGeometry( 'GEOMETRYCOLLECTION(POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10 )))' ) ] }] ]) np.testing.assert_array_equal(v.data().values, [[1, 2, 3, 4]]) v = Variable(tree=[ 'sfilter', [{ 'values': np.array([[1, 2, 3, 4], [5, 6, 7, 8]]), 'spatial_key': [f1, f2], 'temporal_key': [] }, { 'filter_type': 'exclusive', 'containing_geometries': [ GEOSGeometry( 'GEOMETRYCOLLECTION(POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10 )))' ) ] }] ]) np.testing.assert_array_equal(v.data().values, [[5, 6, 7, 8]])
def test_arithmetic_operators_matrices(): v = Variable(tree=[ '+', [ _matrix_val({ 'values': np.array([[1, 2], [3, 4]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }), _matrix_val({ 'values': np.array([[2, 3], [4, 5]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }) ] ]) np.testing.assert_array_equal(v.data().values, np.array([[3, 5], [7, 9]])) v = Variable(tree=[ '+', [ _matrix_val({ 'values': np.array([[1, 2], [3, 4]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }), _scalar_val(2) ] ]) np.testing.assert_array_equal(v.data().values, np.array([[3, 4], [5, 6]])) v = Variable(tree=[ '+', [ _matrix_val({ 'values': np.array([[1, 2], [3, 4]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }), _matrix_val({ 'values': np.array([[1, 2, 3], [4, 5, 6]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key3 }) ] ]) np.testing.assert_array_equal( v.data().values, np.array([ [2., 4., float('nan')], [7., 9., float('nan')], ])) with pytest.raises(ValueError): v = Variable(tree=[ '+', [ _matrix_val({ 'values': np.array([[1, 2], [3, 4]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }), _matrix_val({ 'values': np.array([[1, 2, 3], [4, 5, 6]]), 'spatial_key': spatial_key3, 'temporal_key': temporal_key3 }) ] ]) print v.data().values
def test_arithmetic_operators_scalar(): v = Variable(tree=['+', [_scalar_val(1), _scalar_val(2)]]) assert v.data() == _scalar_val(3) v = Variable( tree=['+', [['+', [_scalar_val(1), _scalar_val(2)]], _scalar_val(3)]]) assert v.data() == _scalar_val(6) v = Variable( tree=['+', [_scalar_val(3), ['+', [_scalar_val(1), _scalar_val(2)]]]]) assert v.data() == _scalar_val(6) v = Variable( tree=['-', [['-', [_scalar_val(6), _scalar_val(2)]], _scalar_val(1)]]) assert v.data() == _scalar_val(3) v = Variable( tree=['*', [_scalar_val(3), ['*', [_scalar_val(1), _scalar_val(3)]]]]) assert v.data() == _scalar_val(9) v = Variable(tree=['/', [_scalar_val(3), _scalar_val(1)]]) assert v.data() == _scalar_val(3)
def test_value_filter_operator(): test_matrix = _matrix_val({ 'values': np.array([[1, 2], [3, 4]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }) v = Variable( tree=['filter', [test_matrix, { 'comparison': '>', 'comparator': 3 }]]) np.testing.assert_array_equal( v.data().values, [[float('nan'), float('nan')], [float('nan'), 4]]) v = Variable( tree=['filter', [test_matrix, { 'comparison': '>=', 'comparator': 3 }]]) np.testing.assert_array_equal(v.data().values, [[float('nan'), float('nan')], [3, 4]]) v = Variable( tree=['filter', [test_matrix, { 'comparison': '<', 'comparator': 3 }]]) np.testing.assert_array_equal( v.data().values, [[1, 2], [float('nan'), float('nan')]]) v = Variable( tree=['filter', [test_matrix, { 'comparison': '<=', 'comparator': 3 }]]) np.testing.assert_array_equal(v.data().values, [[1, 2], [3, float('nan')]]) v = Variable( tree=['filter', [test_matrix, { 'comparison': '==', 'comparator': 3 }]]) np.testing.assert_array_equal( v.data().values, [[float('nan'), float('nan')], [3, float('nan')]]) masked_matrix = _matrix_val({ 'values': ma.array([[float('nan'), float('nan')], [3, float('nan')]]), 'spatial_key': spatial_key, 'temporal_key': temporal_key }) v = Variable(tree=[ 'filter', [masked_matrix, { 'comparison': '==', 'comparator': 2 }] ]) np.testing.assert_array_equal(v.data().values, [[float('nan'), float('nan')], [float('nan'), float('nan')]])
def test_join_operator(set_schema, monkeypatch): with mock.patch('django.db.connection') as connection: connection.schema_name = 'test' connection.cursor.return_value.fetchall.return_value = [ (1, None, None, { 'tmin': 2 }, DateRange(date(2010, 1, 1), date(2010, 1, 1))), (2, None, None, { 'tmin': 5 }, DateRange(date(2010, 1, 2), date(2010, 1, 2))), (1, None, None, { 'tmin': 4 }, DateRange(date(2010, 1, 1), date(2010, 1, 1))), (2, None, None, { 'tmin': 8 }, DateRange(date(2010, 1, 2), date(2010, 1, 2))), ] v = Variable(tree=[ 'join', [{ 'type': 'Layer', 'id': 1, 'name': 'cnty24k97', 'field': 'fid' }, { 'type': 'Table', 'id': 1, 'name': 'cnty24k97_data', 'field': 'fid' }, 'tmin'] ]) np.testing.assert_array_equal(v.data().values, np.array([[2, 5], [4, 8]])) v = Variable(tree=[ 'join', [{ 'type': 'Table', 'id': 1, 'name': 'cnty24k97_data', 'field': 'fid' }, { 'type': 'Layer', 'id': 1, 'name': 'cnty24k97', 'field': 'fid' }, 'tmin'] ]) np.testing.assert_array_equal(v.data().values, np.array([[2, 5], [4, 8]])) # THIS JOIN only works between tables and layers v = Variable(tree=[ 'join', [{ 'type': 'Table', 'id': 1, 'field': 'fid' }, { 'type': 'Table', 'id': 1, 'field': 'fid' }, 'test'] ]) with pytest.raises(ValueError): v.data() v = Variable(tree=[ 'join', [{ 'type': 'Layer', 'id': 1, 'field': 'fid' }, { 'type': 'Layer', 'id': 1, 'field': 'fid' }, 'test'] ]) with pytest.raises(ValueError): v.data()
def test_select_join_operator(set_schema, monkeypatch): with mock.patch('django.db.connection') as connection: connection.schema_name = 'test' connection.cursor.return_value.description.__iter__.return_value = ( ('shaid', 23, None, 4, None, None, None), ('record_id', 23, None, 4, None, None, None), ('date_range', 1082, None, 4, None, None, None), ('tmin', 25, None, -1, None, None, None)) connection.cursor.return_value.fetchall.return_value = [ (1, 1, DateRange(date(2010, 1, 1), date(2010, 1, 1)), 2), (1, 2, DateRange(date(2010, 1, 2), date(2010, 1, 2)), 5), (2, 1, DateRange(date(2010, 1, 1), date(2010, 1, 1)), 4), (2, 2, DateRange(date(2010, 1, 2), date(2010, 1, 2)), 8), ] v = Variable(tree=[ 'select', [ [ 'join', [ { 'type': 'Layer', 'id': 26, 'field': 'fid' }, { 'type': 'Table', 'id': 30, 'field': 'fid' }, ] ], 'tmin', ] ]) data = v.data() np.testing.assert_array_equal(data.values, np.array([[2, 5], [4, 8]])) v = Variable(tree=[ 'select', [[ 'join', [ { 'type': 'Layer', 'id': 26, 'field': 'fid' }, { 'type': 'Table', 'id': 30, 'field': 'fid' }, ] ], 'tmin'] ]) np.testing.assert_array_equal(v.data().values, np.array([[2, 5], [4, 8]])) v = Variable(tree=[ 'select', [[ 'join', [ { 'type': 'Table', 'id': 26, 'field': 'fid' }, { 'type': 'Table', 'id': -1, 'field': 'fid' }, ] ], 'test'] ]) with pytest.raises(ObjectDoesNotExist): v.data() v = Variable(tree=[ 'select', [[ 'join', [ { 'type': 'Layer', 'id': 1, 'field': 'fid' }, { 'type': 'Layer', 'name': "", 'field': 'fid' }, ] ], 'test'] ]) with pytest.raises(ObjectDoesNotExist): v.data()