<commit_before>'''
Unittests for PyRate shared utility objects.
Created on 12/09/2012
@author: <NAME>, NCI
'''

import os
import shutil
import unittest
from os.path import join, basename
from stat import S_IRGRP, S_IWGRP, S_IWOTH, S_IROTH, S_IRUSR, S_IWUSR

from itertools import product
from numpy import isnan, where, nan
from numpy.testing import assert_array_equal

from gdal import Open, Dataset, UseExceptions
UseExceptions()

from pyrate.shared import Ifg, DEM, RasterException
from common import SYD_TEST_TIF, SYD_TEST_DEM_TIF


class IfgTests(unittest.TestCase):
	'''Unit tests for the Ifg/interferogram class.'''

	def setUp(self):
		self.ifg = Ifg(join(SYD_TEST_TIF, 'geo_060619-061002.tif'))
		self.ifg.open()

	def test_headers_as_attr(self):
		for a in ['ncols', 'nrows', 'x_first', 'x_step',
				'y_first', 'y_step', 'wavelength', 'master', 'slave']:
			self.assertTrue(getattr(self.ifg, a) is not None)

	def test_convert_to_nans(self):
		self.ifg.convert_to_nans(0)
		self.assertTrue(self.ifg.nan_converted)

	def test_xylast(self):
		# ensure the X|Y_LAST header element has been created
		self.assertAlmostEqual(self.ifg.x_last, 150.9491667)
		self.assertAlmostEqual(self.ifg.y_last, -34.23)

	def test_num_cells(self):
		# test cell size from header elements
		data = self.ifg.phase_band.ReadAsArray()
		ys, xs = data.shape
		exp_ncells = ys * xs
		self.assertEqual(exp_ncells, self.ifg.num_cells)

	def test_shape(self):
		self.assertEqual(self.ifg.shape, self.ifg.phase_data.shape)

	def test_nan_count(self):
		num_nan = 0
		for row in self.ifg.phase_data:
			for v in row:
				if isnan(v):
					num_nan += 1

		self.assertEqual(num_nan, self.ifg.nan_count)

	def test_phase_band(self):
		data = self.ifg.phase_band.ReadAsArray()
		self.assertEqual(data.shape, (72, 47) )

	def test_nan_fraction(self):
		# NB: source data lacks 0 -> NaN conversion
		data = self.ifg.phase_data
		data = where(data == 0, nan, data) # fake 0 -> nan for the count below

		# manually count # nan cells
		nans = 0
		ys, xs = data.shape
		for y, x in product(xrange(ys), xrange(xs)):
			if isnan(data[y, x]):
				nans += 1
		del data

		num_cells = float(ys * xs)
		self.assertTrue(nans > 0)
		self.assertTrue(nans <= num_cells)
		self.assertEqual(nans / num_cells, self.ifg.nan_fraction)

	def test_xy_size(self):
		self.assertFalse(self.ifg.ncols is None)
		self.assertFalse(self.ifg.nrows is None)

		# test with tolerance from base 90m cell
		self.assertTrue(self.ifg.y_size > 88.0) # within 2% of cells over sydney?
		self.assertTrue(self.ifg.y_size < 92.0, 'Got %s' % self.ifg.y_size)

		syd_width = 76.9 # from nearby pirate coords
		self.assertTrue(self.ifg.x_size > 0.97 * syd_width) # ~3% tolerance
		self.assertTrue(self.ifg.x_size < 1.03 * syd_width)

	def test_centre_latlong(self):
		lat_exp = self.ifg.y_first + ((self.ifg.nrows / 2) * self.ifg.y_step)
		long_exp = self.ifg.x_first + ((self.ifg.ncols / 2) * self.ifg.x_step)
		self.assertEqual(lat_exp, self.ifg.lat_centre)
		self.assertEqual(long_exp, self.ifg.long_centre)

	def test_centre_cell(self):
		self.assertEqual(self.ifg.x_centre, 23)
		self.assertEqual(self.ifg.y_centre, 36)

	def test_time_span(self):
		self.assertEqual(self.ifg.time_span, 0.287474332649)

	def test_wavelength(self):
		self.assertEqual(self.ifg.wavelength, 0.0562356424)


class IfgIOTests(unittest.TestCase):

	def setUp(self):
		self.ifg = Ifg(join(SYD_TEST_TIF, 'geo_060619-061002.tif'))

	def test_open(self):
		self.assertTrue(self.ifg.dataset is None)
		self.assertTrue(self.ifg.is_open is False)
		self.ifg.open(readonly=True)
		self.assertTrue(self.ifg.dataset is not None)
		self.assertTrue(self.ifg.is_open is True)
		self.assertTrue(isinstance(self.ifg.dataset, Dataset))

		# ensure open cannot be called twice
		self.failUnlessRaises(RasterException, self.ifg.open, True)

	def test_write(self):<commit_msg>ifg.time_span is now calculated from the master and slave dates.<commit_after>'''
Unittests for PyRate shared utility objects.
Created on 12/09/2012
@author: <NAME>, NCI
'''

import os
import shutil
import unittest
from os.path import join, basename
from stat import S_IRGRP, S_IWGRP, S_IWOTH, S_IROTH, S_IRUSR, S_IWUSR

from itertools import product
from numpy import isnan, where, nan
from numpy.testing import assert_array_equal

from gdal import Open, Dataset, UseExceptions
UseExceptions()

from pyrate.shared import Ifg, DEM, RasterException
from common import SYD_TEST_TIF, SYD_TEST_DEM_TIF


class IfgTests(unittest.TestCase):
	'''Unit tests for the Ifg/interferogram class.'''

	def setUp(self):
		self.ifg = Ifg(join(SYD_TEST_TIF, 'geo_060619-061002.tif'))
		self.ifg.open()

	def test_headers_as_attr(self):
		for a in ['ncols', 'nrows', 'x_first', 'x_step',
				'y_first', 'y_step', 'wavelength', 'master', 'slave']:
			self.assertTrue(getattr(self.ifg, a) is not None)

	def test_convert_to_nans(self):
		self.ifg.convert_to_nans(0)
		self.assertTrue(self.ifg.nan_converted)

	def test_xylast(self):
		# ensure the X|Y_LAST header element has been created
		self.assertAlmostEqual(self.ifg.x_last, 150.9491667)
		self.assertAlmostEqual(self.ifg.y_last, -34.23)

	def test_num_cells(self):
		# test cell size from header elements
		data = self.ifg.phase_band.ReadAsArray()
		ys, xs = data.shape
		exp_ncells = ys * xs
		self.assertEqual(exp_ncells, self.ifg.num_cells)

	def test_shape(self):
		self.assertEqual(self.ifg.shape, self.ifg.phase_data.shape)

	def test_nan_count(self):
		num_nan = 0
		for row in self.ifg.phase_data:
			for v in row:
				if isnan(v):
					num_nan += 1

		self.assertEqual(num_nan, self.ifg.nan_count)

	def test_phase_band(self):
		data = self.ifg.phase_band.ReadAsArray()
		self.assertEqual(data.shape, (72, 47) )

	def test_nan_fraction(self):
		# NB: source data lacks 0 -> NaN conversion
		data = self.ifg.phase_data
		data = where(data == 0, nan, data) # fake 0 -> nan for the count below

		# manually count # nan cells
		nans = 0
		ys, xs = data.shape
		for y, x in product(xrange(ys), xrange(xs)):
			if isnan(data[y, x]):
				nans += 1
		del data

		num_cells = float(ys * xs)
		self.assertTrue(nans > 0)
		self.assertTrue(nans <= num_cells)
		self.assertEqual(nans / num_cells, self.ifg.nan_fraction)

	def test_xy_size(self):
		self.assertFalse(self.ifg.ncols is None)
		self.assertFalse(self.ifg.nrows is None)

		# test with tolerance from base 90m cell
		self.assertTrue(self.ifg.y_size > 88.0) # within 2% of cells over sydney?
		self.assertTrue(self.ifg.y_size < 92.0, 'Got %s' % self.ifg.y_size)

		syd_width = 76.9 # from nearby pirate coords
		self.assertTrue(self.ifg.x_size > 0.97 * syd_width) # ~3% tolerance
		self.assertTrue(self.ifg.x_size < 1.03 * syd_width)

	def test_centre_latlong(self):
		lat_exp = self.ifg.y_first + ((self.ifg.nrows / 2) * self.ifg.y_step)
		long_exp = self.ifg.x_first + ((self.ifg.ncols / 2) * self.ifg.x_step)
		self.assertEqual(lat_exp, self.ifg.lat_centre)
		self.assertEqual(long_exp, self.ifg.long_centre)

	def test_centre_cell(self):
		self.assertEqual(self.ifg.x_centre, 23)
		self.assertEqual(self.ifg.y_centre, 36)

	def test_time_span(self):
		self.assertAlmostEqual(self.ifg.time_span, 0.287474332649)

	def test_wavelength(self):
		self.assertEqual(self.ifg.wavelength, 0.0562356424)


class IfgIOTests(unittest.TestCase):

	def setUp(self):
		self.ifg = Ifg(join(SYD_TEST_TIF, 'geo_060619-061002.tif'))

	def test_open(self):
		self.assertTrue(self.ifg.dataset is None)
		self.assertTrue(self.ifg.is_open is False)
		self.ifg.open(readonly=True)
		self.assertTrue(self.ifg.dataset is not None)
		self.assertTrue(self.ifg.is_open is True)
		self.assertTrue(isinstance(self.ifg.dataset, Dataset))

		# ensure open cannot be called twice
		self.failUnlessRaises(RasterException, self.ifg.open, True)

	def test_write(self):
{'fraction_non_alphanumeric': 0.12101012690680682, 'fraction_numerical': 0.027688757851557493, 'mean_word_length': 4.448324022346369, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '7363766', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3020, 'n_words': 662}