def test_datetimelike_bounded_cell(self): point = object() cell = Cell(point=object(), bound=[mock.Mock(timetuple=mock.Mock()), mock.Mock(timetuple=mock.Mock())]) with self.assertRaisesRegexp(TypeError, 'bounded region for datetime'): cell.contains_point(point)
def test_netcdftime_other(self): # Check that cell comparison to a netcdftime.datetime object # raises an exception otherwise this will fall back to id comparison # producing unreliable results. dt = netcdftime.datetime(2010, 3, 21) cell = Cell(mock.Mock(timetuple=mock.Mock())) self.assert_raises_on_comparison(cell, dt, TypeError, 'determine the order of netcdftime')
def test_PartialDateTime_unbounded_cell(self): # Check that cell comparison works with PartialDateTimes. dt = PartialDateTime(month=6) cell = Cell(netcdftime.datetime(2010, 3, 1)) self.assertLess(cell, dt) self.assertGreater(dt, cell) self.assertLessEqual(cell, dt) self.assertGreaterEqual(dt, cell)
def test_cell_from_multidim_coord(self): Cell = iris.coords.Cell coord = iris.coords.AuxCoord(points=np.arange(12).reshape(3, 4), long_name='test', units='1', bounds=np.arange(48).reshape(3, 4, 4)) self.assertRaises(IndexError, coord.cell, 0) self.assertEqual(Cell(point=3, bound=(12, 13, 14, 15)), coord.cell((0, 3)))
def test_len_1_numpy_array(self): # Check that cell comparison works with numpy arrays of len=1 cell = Cell(1.3) self.assertGreater(np.array([1.5]), cell) self.assertLess(np.array([1.1]), cell) self.assertGreaterEqual(np.array([1.3]), cell) self.assertLessEqual(np.array([1.3]), cell)
def test_0D_numpy_array(self): # Check that cell comparison works with 0D numpy arrays cell = Cell(1.3) self.assertGreater(np.array(1.5), cell) self.assertLess(np.array(1.1), cell) self.assertGreaterEqual(np.array(1.3), cell) self.assertLessEqual(np.array(1.3), cell)
def test_datetime_unbounded_cell(self): # Check that cell comparison works with datetimes. dt = datetime.datetime(2000, 6, 15) cell = Cell(datetime.datetime(2000, 1, 1)) # Note the absence of the inverse of these # e.g. self.assertGreater(dt, cell). # See http://bugs.python.org/issue8005 self.assertLess(cell, dt) self.assertLessEqual(cell, dt)
def test_overlap_order(self): # Test cells that overlap still sort correctly. cells = [Cell(point=375804.0, bound=(375792.0, 375816.0)), Cell(point=375672.0, bound=(375660.0, 375684.0)), Cell(point=375792.0, bound=(375780.0, 375804.0)), Cell(point=375960.0, bound=(375948.0, 375972.0))] sorted_cells = [Cell(point=375672.0, bound=(375660.0, 375684.0)), Cell(point=375792.0, bound=(375780.0, 375804.0)), Cell(point=375804.0, bound=(375792.0, 375816.0)), Cell(point=375960.0, bound=(375948.0, 375972.0))] self.assertEqual(sorted(cells), sorted_cells)
def test_cell_from_coord(self): Cell = iris.coords.Cell coord = iris.coords.AuxCoord(np.arange(4) * 1.5, long_name='test', units='1') self.assertEqual(Cell(point=0.0, bound=None), coord.cell(0)) self.assertEqual(Cell(point=1.5, bound=None), coord.cell(1)) self.assertEqual(Cell(point=4.5, bound=None), coord.cell(-1)) self.assertEqual(Cell(point=3.0, bound=None), coord.cell(-2)) # put bounds on the regular coordinate coord.guess_bounds() self.assertEqual(Cell(point=0.0, bound=(-0.75, 0.75)), coord.cell(0)) self.assertEqual(Cell(point=1.5, bound=(0.75, 2.25)), coord.cell(1)) self.assertEqual(Cell(point=4.5, bound=(3.75, 5.25)), coord.cell(-1)) self.assertEqual(Cell(point=3.0, bound=(2.25, 3.75)), coord.cell(-2)) self.assertEqual(Cell(point=4.5, bound=(3.75, 5.25)), coord.cell(slice(-1, None)))
def test_PartialDateTime_bounded_cell(self): # Check that bounded comparisions to a PartialDateTime # raise an exception. These are not supported as they # depend on the calendar. dt = PartialDateTime(month=6) cell = Cell(datetime.datetime(2010, 1, 1), bound=[datetime.datetime(2010, 1, 1), datetime.datetime(2011, 1, 1)]) self.assert_raises_on_comparison(cell, dt, TypeError, 'bounded region for datetime')
def test_datetimelike_bounded_cell(self): # Check that equality with a datetime-like bounded cell # raises an error. This is not supported as it # depends on the calendar which is not always known from # the datetime-like bound objects. other = mock.Mock(timetuple=mock.Mock()) cell = Cell(point=object(), bound=[mock.Mock(timetuple=mock.Mock()), mock.Mock(timetuple=mock.Mock())]) with self.assertRaisesRegexp(TypeError, 'bounded region for datetime'): cell == other
def test_comparison_numeric_with_bounds(self): # Check what happens when you compare a simple number with a # point-and-bound Cell. self._check_permutations(7, Cell(10, [8, 12]), True, True, False) self._check_permutations(8, Cell(10, [8, 12]), False, True, True) self._check_permutations(9, Cell(10, [8, 12]), False, True, True) self._check_permutations(10, Cell(10, [8, 12]), False, True, True) self._check_permutations(11, Cell(10, [8, 12]), False, True, True) self._check_permutations(12, Cell(10, [8, 12]), False, True, True) self._check_permutations(13, Cell(10, [8, 12]), False, False, False)
def test_netcdftime_cell(self): # Check that cell comparison when the cell contains # netcdftime.datetime objects raises an exception otherwise # this will fall back to id comparison producing unreliable # results. cell = Cell(netcdftime.datetime(2010, 3, 21)) dt = mock.Mock(timetuple=mock.Mock()) self.assert_raises_on_comparison(cell, dt, TypeError, 'determine the order of netcdftime') self.assert_raises_on_comparison(cell, 23, TypeError, 'determine the order of netcdftime') self.assert_raises_on_comparison(cell, 'hello', TypeError, 'Unexpected type.*str')
def test_datetime_ordering(self): # Check that cell comparison works with objects with a "timetuple". dt = mock.Mock(timetuple=mock.Mock()) cell = Cell(datetime.datetime(2010, 3, 21)) with mock.patch('operator.gt') as gt: _ = cell > dt gt.assert_called_once_with(cell.point, dt) # Now check that the existence of timetuple is causing that. del dt.timetuple with self.assertRaisesRegexp(ValueError, 'Unexpected type of other <(.*)>'): _ = cell > dt
def test_cell_lhs(self): cell = Cell(point=1.5) n = np.float64(1.2) try: bool(cell < n) bool(cell <= n) bool(cell > n) bool(cell >= n) bool(cell == n) bool(cell != n) except: self.fail( "Result of comparison could not be used as a truth value")
def test_cell_rhs(self): cell = Cell(point=1.5) n = np.float64(1.2) try: bool(n < cell) bool(n <= cell) bool(n > cell) bool(n >= cell) bool(n == cell) bool(n != cell) except: self.fail( "Result of comparison could not be used as a truth value")
def test_datetimelike_point(self): point = mock.Mock(timetuple=mock.Mock()) cell = Cell(point=object(), bound=[object(), object()]) with self.assertRaisesRegex(TypeError, "bounded region for datetime"): cell.contains_point(point)
def test_comparison_numeric(self): # Check what happens when you compare a simple number with a # point-only Cell. self._check_permutations(9, Cell(10), True, True, False) self._check_permutations(10, Cell(10), False, True, True) self._check_permutations(11, Cell(10), False, False, False)
def test_PartialDateTime_other(self): cell = Cell(datetime.datetime(2010, 3, 2)) # A few simple cases. self.assertEqual(cell, PartialDateTime(month=3)) self.assertNotEqual(cell, PartialDateTime(month=3, hour=12)) self.assertNotEqual(cell, PartialDateTime(month=4))