<commit_before>'''
Collection of tests for orbital correction.

Created on 31/3/13
@author: <NAME>
'''


import unittest
from os.path import join

from numpy.linalg import pinv
from numpy import nan, isnan, array, reshape, float32
from numpy import empty, ones, zeros, meshgrid, dot
from numpy.testing import assert_array_equal, assert_array_almost_equal

import algorithm
from shared import Ifg
from orbital import OrbitalError, orbital_correction
from orbital import get_design_matrix, get_network_design_matrix
from orbital import INDEPENDENT_METHOD, NETWORK_METHOD, PLANAR, QUADRATIC
from tests_common import sydney5_mock_ifgs, MockIfg, IFMS5


# FIXME: check the offset cols as 1s in network tests

# TODO: test lstsq() here against Hua's manual method

# ORDER of work:
# fix up offsets options for QUAD DM tests
# multilooking (do ML at start when multilooking the originals)
# high level functions/workflow


class SingleDesignMatrixTests(unittest.TestCase):
	'''
	Unittests verify correctness of basic planar & quadratic design matricies	or
	DMs. This class serves two purposes, ensuring the independent method DMs are
	produced correctly. Secondly, these indivdual DMs are subsets of the larger
	DM 'grid' required for the networked orbital correction method.
	'''

	def setUp(self):
		# fake cell sizes
		self.xs = 0.75
		self.ys = 0.8
		self.ifg = Ifg("../../tests/sydney_test/obs/geo_060619-061002.unw")
		self.ifg.open()

		self.m = MockIfg(self.ifg, 3, 4)
		self.m.X_SIZE = self.xs
		self.m.Y_SIZE = self.ys


	def test_create_planar_dm(self):
		# verify basic planar DM without offsets
		exp = unittest_dm(self.m, INDEPENDENT_METHOD, PLANAR, False)
		self.assertEqual(len(exp), self.m.num_cells)
		self.assertEqual(len(exp[0]), 2) # expecting 2 cols
		assert_array_almost_equal(exp, get_design_matrix(self.m, PLANAR, False))


	def test_create_planar_dm_offsets(self):
		# verify planar DM with offsets
		exp = unittest_dm(self.m, INDEPENDENT_METHOD, PLANAR, True)
		self.assertEqual(len(exp), self.m.num_cells)
		self.assertEqual(len(exp[0]), 3) # expecting x,y,offsets cols
		assert_array_almost_equal(exp, get_design_matrix(self.m, PLANAR, True))


	def test_create_planar_dm_network(self):
		# networked method planar version should not have offsets col
		exp = unittest_dm(self.m, NETWORK_METHOD, PLANAR, False)
		self.assertEqual(len(exp), self.m.num_cells)
		self.assertEqual(len(exp[0]), 2) # expecting 2 cols

		exp2 = unittest_dm(self.m, NETWORK_METHOD, PLANAR, True)
		self.assertEqual(len(exp2), self.m.num_cells)
		self.assertEqual(len(exp2[0]), 2) # still 2, offsets cols included elsewhere

		assert_array_equal(exp, exp2)


	# FIXME: expand this as per tests for PLANAR
	def test_design_matrix_quadratic(self):
		# use exp & subset of exp to test against both forms of DM
		exp = unittest_dm(self.m, INDEPENDENT_METHOD, QUADRATIC, True)
		design_mat = get_design_matrix(self.m, QUADRATIC, False) # no offset
		assert_array_almost_equal(exp[:, :-1], design_mat)
		assert_array_almost_equal(exp, get_design_matrix(self.m, QUADRATIC, True))



class OrbitalCorrection(unittest.TestCase):
	'''Test cases for the orbital correction component of PyRate.'''<commit_msg>Added unittests to verify creation of basic quadratic DMs.<commit_after>'''
Collection of tests for orbital correction.

Created on 31/3/13
@author: <NAME>
'''


import unittest
from os.path import join

from numpy.linalg import pinv
from numpy import nan, isnan, array, reshape, float32
from numpy import empty, ones, zeros, meshgrid, dot
from numpy.testing import assert_array_equal, assert_array_almost_equal

import algorithm
from shared import Ifg
from orbital import OrbitalError, orbital_correction
from orbital import get_design_matrix, get_network_design_matrix
from orbital import INDEPENDENT_METHOD, NETWORK_METHOD, PLANAR, QUADRATIC
from tests_common import sydney5_mock_ifgs, MockIfg, IFMS5


# FIXME: check the offset cols as 1s in network tests

# TODO: test lstsq() here against Hua's manual method

# ORDER of work:
# fix up offsets options for QUAD DM tests
# multilooking (do ML at start when multilooking the originals)
# high level functions/workflow


class SingleDesignMatrixTests(unittest.TestCase):
	'''
	Unittests verify correctness of basic planar & quadratic design matricies	or
	DMs. This class serves two purposes, ensuring the independent method DMs are
	produced correctly. Secondly, these indivdual DMs are subsets of the larger
	DM 'grid' required for the networked orbital correction method.
	'''

	def setUp(self):
		# fake cell sizes
		self.xs = 0.75
		self.ys = 0.8
		self.ifg = Ifg("../../tests/sydney_test/obs/geo_060619-061002.unw")
		self.ifg.open()

		self.m = MockIfg(self.ifg, 3, 4)
		self.m.X_SIZE = self.xs
		self.m.Y_SIZE = self.ys


	# FIXME: test the actual results, not the ones from the test func!
	def test_create_planar_dm(self):
		# verify basic planar DM without offsets
		exp = unittest_dm(self.m, INDEPENDENT_METHOD, PLANAR, False)
		self.assertEqual(len(exp), self.m.num_cells) # TODO: refactor to test shape
		self.assertEqual(len(exp[0]), 2) # expecting 2 cols
		assert_array_almost_equal(exp, get_design_matrix(self.m, PLANAR, False))


	def test_create_planar_dm_offsets(self):
		# verify planar DM with offsets
		exp = unittest_dm(self.m, INDEPENDENT_METHOD, PLANAR, True)
		self.assertEqual(len(exp), self.m.num_cells) # TODO: refactor to test shape
		self.assertEqual(len(exp[0]), 3) # expecting x,y,offsets cols
		assert_array_almost_equal(exp, get_design_matrix(self.m, PLANAR, True))


	def test_create_planar_dm_network(self):
		# networked method planar version should not have offsets col
		exp = unittest_dm(self.m, NETWORK_METHOD, PLANAR, False)
		self.assertEqual(len(exp), self.m.num_cells) # TODO: refactor to test shape
		self.assertEqual(len(exp[0]), 2) # expecting 2 cols

		exp2 = unittest_dm(self.m, NETWORK_METHOD, PLANAR, True)
		self.assertEqual(len(exp2), self.m.num_cells) # TODO: refactor to test shape
		self.assertEqual(len(exp2[0]), 2) # still 2, offsets cols included elsewhere

		assert_array_equal(exp, exp2)


	# tests for quadratic mode

	def test_create_quadratic_dm(self):
		offset = False
		act = get_design_matrix(self.m, QUADRATIC, offset)
		self.assertEqual(act.shape, (self.m.num_cells, 5))
		exp = unittest_dm(self.m, INDEPENDENT_METHOD, QUADRATIC, offset)
		assert_array_equal(act, exp)


	def test_create_quadratic_dm_offsets(self):
		offset = True
		act = get_design_matrix(self.m, QUADRATIC, offset)
		self.assertEqual(act.shape, (self.m.num_cells, 6))
		exp = unittest_dm(self.m, INDEPENDENT_METHOD, QUADRATIC, offset)
		assert_array_equal(act, exp)


	def test_create_quadratic_dm_network(self):
		raise NotImplementedError
		# TODO: compare actuals against the test DM
		#assert_array_equal(act, exp)



class OrbitalCorrection(unittest.TestCase):
	'''Test cases for the orbital correction component of PyRate.'''
{'fraction_non_alphanumeric': 0.09121133275513155, 'fraction_numerical': 0.01315409077768141, 'mean_word_length': 5.166666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1179377', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2302, 'n_words': 682}