Exemple #1
0
    def post(self, request):
        """
		Returns the analysis for the requested point.
		
		POST
			id,			# word matrix id
			latitude,	# point coord
			longitude,	# point coord
			method,		# circle or neighbourhood
			parameter	# circle radius or neighbourhood size
		
		200:
			origin,		# language used as refernce (for neighbourhoods)
			d, 			# {} of (global, real)
			p 			# pearson coefficient (the swadeshness)
		
		400: error
		404: error 		# word matrix not found
		"""

        try:
            post = self.validate_post(request.body)
        except ValueError as error:
            return JsonResponse({'error': str(error)}, status=400)

        matrix = WordMatrix()

        try:
            matrix.load(post['id'])
        except ValueError:
            return JsonResponse(
                {'error': 'The file has expired. Please re-upload.'},
                status=404)

        point = Point(post['latitude'], post['longitude'])

        if post['method'] == 'circle':
            try:
                d, p = point.get_swadeshness_in_radius(matrix,
                                                       post['parameter'])
            except MapError as error:
                return JsonResponse({'error': str(error)}, status=400)

            return JsonResponse({'d': d, 'p': p}, status=200)

        else:
            try:
                origin, d, p = point.get_swadeshness_by_nearest(
                    matrix, post['parameter'])
            except MapError as error:
                return JsonResponse({'error': str(error)}, status=400)

            return JsonResponse({'origin': origin, 'd': d, 'p': p}, status=200)
	def setUp(self):
		matrix = WordMatrix()
		
		with open('app/fixtures/berg.tsv', 'r') as f:
			matrix.load_raw(f)
		
		file_id = matrix.save()
		
		self.post = {
			'id': file_id,
			'cells': [[65, -22], [55, 50], [-15, -70]],
			'method': 'circle',
			'parameter': 1000
		}
Exemple #3
0
    def setUp(self):
        matrix = WordMatrix()

        with open('app/fixtures/berg.tsv', 'r') as f:
            matrix.load_raw(f)

        file_id = matrix.save()

        self.post = {
            'id': file_id,
            'latitude': 43,
            'longitude': 42,
            'method': 'circle',
            'parameter': 500
        }
Exemple #4
0
    def test_load_raw(self):
        matrix = WordMatrix()
        matrix.load_raw(self.f)

        self.assertEqual(matrix.d[('sjd', 'sms')], (0.3487, 0.4035))
        self.assertEqual(matrix.d[('kv', 'mhr')], (0.5314, 0.2624))
        self.assertEqual(matrix.d[('mhr', 'udm')], (0.5314, 0.1520))
        self.assertEqual(matrix.d[('ddo', 'sq')], (1.0000, 0.1144))

        for key, value in matrix.d.items():
            self.assertIs(type(key), tuple)
            self.assertEqual(len(key), 2)
            self.assertIs(type(value), tuple)
            self.assertEqual(len(value), 2)

        self.assertEqual(len(matrix.d), 2346)
Exemple #5
0
    def test_good_upload(self):
        with open('app/fixtures/berg.tsv', 'r') as f:
            response = self.client.post(reverse('file_api'), {'file': f})

        self.assertEqual(response.status_code, 200)

        d = read_json(response.content)
        self.assertEqual(len(d), 2)

        self.assertIn('id', d)
        self.assertGreater(len(d['id']), 0)

        self.assertIn('name', d)
        self.assertEqual(d['name'], 'berg.tsv')

        matrix = WordMatrix()
        matrix.load(d['id'])
        self.assertEqual(len(matrix.d), 2346)
Exemple #6
0
	def post(self, request):
		"""
		Returns two-dimensional array of temperatures.
		
		POST
			id,			# word matrix id
			cells,		# [] of [latitude, longitude]
			method,		# circle or neighbourhood
			parameter	# circle radius or neighbourhood size
		
		200:
			cells: [] of [latitude, longitude, temperature]
		
		400: error
		404: error		# file not found
		"""
		
		try:
			post = self.validate_post(request.body)
		except ValueError as error:
			return JsonResponse({'error': str(error)}, status=400)
		
		
		matrix = WordMatrix()
		
		try:
			matrix.load(post['id'])
		except ValueError:
			return JsonResponse({
				'error': 'The file has expired. Please re-upload.'
			}, status=404)
		
		
		honeycomb = Honeycomb(post['cells'])
		
		if post['method'] == 'circle':
			honeycomb.calculate_on_circles(matrix, post['parameter'])
		else:
			honeycomb.calculate_on_neighbourhoods(matrix, post['parameter'])
		
		return JsonResponse({'cells': honeycomb.cells}, status=200)
Exemple #7
0
	def post(self, request):
		"""
		Creates a new word matrix and stores it for subsequent API calls.
		Returns the ID of the matrix.
		
		POST
			file	# the .tsv file
		
		200:
			id,		# word matrix id
			name	# pretty file name
		
		400: error
		"""
		
		try:
			f = self.validate_file(request)
		except ValueError as error:
			return JsonResponse({'error': str(error)}, status=400)
		
		matrix = WordMatrix()
		
		try:
			matrix.load_raw(f)
		except ValueError as error:
			return JsonResponse({'error': str(error)}, status=400)
		except Exception as error:  # csv.Error
			return JsonResponse({'error': 'File unreadable.'}, status=400)
		
		matrix.save()
		
		return JsonResponse({
			'id': matrix.storage_id,
			'name': f.name
		}, status=200)
Exemple #8
0
    def test_get_distances(self):
        matrix = WordMatrix()
        matrix.load_raw(self.f)

        self.assertEqual(matrix.get_distances('sjd', 'sms'), (0.3487, 0.4035))
        self.assertEqual(matrix.get_distances('sms', 'sjd'), (0.3487, 0.4035))

        self.assertEqual(matrix.get_distances('kv', 'mhr'), (0.5314, 0.2624))
        self.assertEqual(matrix.get_distances('mhr', 'kv'), (0.5314, 0.2624))

        self.assertEqual(matrix.get_distances('mhr', 'udm'), (0.5314, 0.1520))
        self.assertEqual(matrix.get_distances('udm', 'mhr'), (0.5314, 0.1520))

        self.assertEqual(matrix.get_distances('ddo', 'sq'), (1.0000, 0.1144))
        self.assertEqual(matrix.get_distances('sq', 'ddo'), (1.0000, 0.1144))
Exemple #9
0
 def setUp(self):
     self.matrix = WordMatrix()
     with open('app/fixtures/berg.tsv', 'r') as f:
         self.matrix.load_raw(f)
Exemple #10
0
class PointTestCase(TestCase):
    fixtures = ['languages.json']

    def setUp(self):
        self.matrix = WordMatrix()
        with open('app/fixtures/berg.tsv', 'r') as f:
            self.matrix.load_raw(f)

    def test_swadeshness_in_radius(self):
        elbrus = Point(43, 42)
        s = elbrus.get_swadeshness_in_radius(self.matrix, 500)

        self.assertEqual(len(s), 2)
        d, p = s

        self.assertEqual(len(d), 8 * 7 / 2)

        lang = ('ab', 'os', 'ka', 'ady', 'ddo', 'ce', 'hy', 'dar')
        for i in lang:
            for j in lang:
                if i == j:
                    continue
                key = [i, j]
                key.sort()
                key = key[0] + ',' + key[1]
                self.assertIn(key, d)

        self.assertLessEqual(p, 1)
        self.assertGreaterEqual(p, -1)

    def test_neighbourhood_swadeshness(self):
        elbrus = Point(43, 42)
        s = elbrus.get_swadeshness_by_nearest(self.matrix, 7)

        self.assertEqual(len(s), 3)
        origin, d, p = s

        self.assertEqual(origin, 'ab')

        lang = ('os', 'ka', 'ady', 'ddo', 'ce', 'hy', 'dar')
        for i in lang:
            self.assertIn(i, d)

        self.assertLessEqual(p, 1)
        self.assertGreaterEqual(p, -1)

    @given(floats(min_value=-90.0, max_value=90.0),
           floats(min_value=-180.0, max_value=180.0),
           integers(min_value=1, max_value=5000))
    def test_circle_does_not_break(self, latitude, longitude, radius):
        point = Point(latitude, longitude)

        try:
            d, p = point.get_swadeshness_in_radius(self.matrix, radius)
        except Exception as error:
            self.assertIsInstance(error, MapError)
        else:
            self.assertIs(type(d), dict)
            self.assertLessEqual(p, 1)
            self.assertGreaterEqual(p, -1)

    @given(floats(min_value=-90.0, max_value=90.0),
           floats(min_value=-180.0, max_value=180.0),
           integers(min_value=1, max_value=42))
    def test_neighbourhood_does_not_break(self, latitude, longitude, k):
        point = Point(latitude, longitude)

        try:
            origin, d, p = point.get_swadeshness_by_nearest(self.matrix, k)
        except Exception as error:
            self.assertIsInstance(error, MapError)
        else:
            self.assertIs(type(d), dict)
            self.assertLessEqual(p, 1)
            self.assertGreaterEqual(p, -1)
Exemple #11
0
    def test_save_and_load(self):
        matrix = WordMatrix()
        matrix.load_raw(self.f)
        storage_id = matrix.save()

        matrix = WordMatrix()
        self.assertEqual(len(matrix.d), 0)

        matrix.load(storage_id)
        self.assertEqual(len(matrix.d), 2346)

        cache.clear()
        self.assertEqual(len(matrix.d), 2346)

        matrix = WordMatrix()
        with self.assertRaises(ValueError):
            matrix.load(storage_id)
Exemple #12
0
    def test_load_raw_error(self):
        matrix = WordMatrix()
        f = open('app/fixtures/languages.tab', 'r')

        with self.assertRaises(ValueError):
            matrix.load_raw(f)
Exemple #13
0
	def setUp(self):
		self.matrix = WordMatrix()
		with open('app/fixtures/berg.tsv', 'r') as f:
			self.matrix.load_raw(f)
		
		self.cells = [[65, -22], [55, 50], [-15, -70]]
Exemple #14
0
class HoneycombTestCase(TestCase):
	fixtures = ['languages.json']
	
	def setUp(self):
		self.matrix = WordMatrix()
		with open('app/fixtures/berg.tsv', 'r') as f:
			self.matrix.load_raw(f)
		
		self.cells = [[65, -22], [55, 50], [-15, -70]]
	
	def tearDown(self):
		cache.clear()
	
	
	def test_calculate_on_circles(self):
		honeycomb = Honeycomb(self.cells)
		
		honeycomb.calculate_on_circles(self.matrix, 1000)
		self.assertEqual(len(honeycomb.cells), len(self.cells))
		
		for key, cell in enumerate(honeycomb.cells):
			self.assertEqual(len(cell), 3)
			self.assertEqual(cell[0], self.cells[key][0])
			self.assertEqual(cell[1], self.cells[key][1])
			self.assertGreaterEqual(cell[2], -1)
			self.assertLessEqual(cell[2], 1)
	
	
	def test_calculate_on_neighbourhoods(self):
		honeycomb = Honeycomb(self.cells)
		
		honeycomb.calculate_on_neighbourhoods(self.matrix, 20)
		self.assertEqual(len(honeycomb.cells), len(self.cells))
		
		for key, cell in enumerate(honeycomb.cells):
			self.assertEqual(len(cell), 3)
			self.assertEqual(cell[0], self.cells[key][0])
			self.assertEqual(cell[1], self.cells[key][1])
			self.assertGreaterEqual(cell[2], -1)
			self.assertLessEqual(cell[2], 1)
	
	
	@given(
		lists(
			elements=tuples(
				floats(min_value=-90.0, max_value=90.0),
				floats(min_value=-180.0, max_value=180.0)
			),
			min_size=0,
			max_size=500
		).map(
			lambda x: [list(i) for i in x]
		),
		integers(min_value=1, max_value=5000)
	)
	def test_circle_does_not_break(self, empty_cells, radius):
		honeycomb = Honeycomb(empty_cells)
		honeycomb.calculate_on_circles(self.matrix, radius)
		
		self.assertEqual(len(honeycomb.cells), len(empty_cells))
		
		for key, cell in enumerate(honeycomb.cells):
			self.assertEqual(len(cell), 3)
			self.assertEqual(cell[0], empty_cells[key][0])
			self.assertEqual(cell[1], empty_cells[key][1])
			self.assertGreaterEqual(cell[2], -1)
			self.assertLessEqual(cell[2], 1)
	
	
	@given(
		lists(
			elements=tuples(
				floats(min_value=-90.0, max_value=90.0),
				floats(min_value=-180.0, max_value=180.0)
			),
			min_size=0,
			max_size=500
		).map(
			lambda x: [list(i) for i in x]
		),
		integers(min_value=1, max_value=20)
	)
	def test_neighbourhood_does_not_break(self, empty_cells, k):
		honeycomb = Honeycomb(empty_cells)
		honeycomb.calculate_on_neighbourhoods(self.matrix, k)
		
		self.assertEqual(len(honeycomb.cells), len(empty_cells))
		
		for key, cell in enumerate(honeycomb.cells):
			self.assertEqual(len(cell), 3)
			self.assertEqual(cell[0], empty_cells[key][0])
			self.assertEqual(cell[1], empty_cells[key][1])
			self.assertGreaterEqual(cell[2], -1)
			self.assertLessEqual(cell[2], 1)