1- # (C) British Crown Copyright 2010 - 2018 , Met Office
1+ # (C) British Crown Copyright 2010 - 2019 , Met Office
22#
33# This file is part of Iris.
44#
@@ -411,17 +411,20 @@ class TestAggregators(tests.IrisTest):
411411 def _check_collapsed_percentile (self , cube , percents , collapse_coord ,
412412 expected_result , CML_filename = None ,
413413 ** kwargs ):
414+ cube_data_type = type (cube .data )
414415 expected_result = np .array (expected_result , dtype = np .float32 )
415416 result = cube .collapsed (collapse_coord , iris .analysis .PERCENTILE ,
416417 percent = percents , ** kwargs )
417418 np .testing .assert_array_almost_equal (result .data , expected_result )
419+ self .assertEqual (type (result .data ), cube_data_type )
418420 if CML_filename is not None :
419421 self .assertCML (result , ('analysis' , CML_filename ), checksum = False )
420422
421423 def _check_percentile (self , data , axis , percents , expected_result ,
422424 ** kwargs ):
423425 result = iris .analysis ._percentile (data , axis , percents , ** kwargs )
424426 np .testing .assert_array_almost_equal (result , expected_result )
427+ self .assertEqual (type (result ), type (expected_result ))
425428
426429 def test_percentile_1d_25_percent (self ):
427430 cube = tests .stock .simple_1d ()
@@ -445,6 +448,13 @@ def test_fast_percentile_1d_75_percent(self):
445448 cube , 75 , 'foo' , 7.5 , fast_percentile_method = True ,
446449 CML_filename = 'third_quartile_foo_1d_fast_percentile.cml' )
447450
451+ def test_fast_percentile_1d_75_percent_masked_type_no_mask (self ):
452+ cube = tests .stock .simple_1d ()
453+ cube .data = ma .MaskedArray (cube .data )
454+ self ._check_collapsed_percentile (
455+ cube , 75 , 'foo' , 7.5 , fast_percentile_method = True ,
456+ CML_filename = 'third_quartile_foo_1d_fast_percentile.cml' )
457+
448458 def test_percentile_2d_single_coord (self ):
449459 cube = tests .stock .simple_2d ()
450460 self ._check_collapsed_percentile (
@@ -469,6 +479,20 @@ def test_fast_percentile_2d_two_coords(self):
469479 cube , 25 , ['foo' , 'bar' ], [2.75 ], fast_percentile_method = True ,
470480 CML_filename = 'first_quartile_foo_bar_2d_fast_percentile.cml' )
471481
482+ def test_fast_percentile_2d_single_coord_masked_type_no_mask (self ):
483+ cube = tests .stock .simple_2d ()
484+ cube .data = ma .MaskedArray (cube .data )
485+ self ._check_collapsed_percentile (
486+ cube , 25 , 'foo' , [0.75 , 4.75 , 8.75 ], fast_percentile_method = True ,
487+ CML_filename = 'first_quartile_foo_2d_fast_percentile.cml' )
488+
489+ def test_fast_percentile_2d_two_coords_masked_type_no_mask (self ):
490+ cube = tests .stock .simple_2d ()
491+ cube .data = ma .MaskedArray (cube .data )
492+ self ._check_collapsed_percentile (
493+ cube , 25 , ['foo' , 'bar' ], [2.75 ], fast_percentile_method = True ,
494+ CML_filename = 'first_quartile_foo_bar_2d_fast_percentile.cml' )
495+
472496 def test_percentile_3d (self ):
473497 array_3d = np .arange (24 , dtype = np .int32 ).reshape ((2 , 3 , 4 ))
474498 expected_result = np .array ([[6. , 7. , 8. , 9. ],
@@ -503,6 +527,16 @@ def test_fast_percentile_3d_axis_one(self):
503527 self ._check_percentile (array_3d , 1 , 50 , expected_result ,
504528 fast_percentile_method = True )
505529
530+ def test_fast_percentile_3d_axis_one_masked_type_no_mask (self ):
531+ array_3d = np .arange (24 , dtype = np .int32 ).reshape ((2 , 3 , 4 ))
532+ array_3d = np .ma .MaskedArray (array_3d )
533+ expected_result = ma .MaskedArray ([[4. , 5. , 6. , 7. ],
534+ [16. , 17. , 18. , 19. ]],
535+ dtype = np .float32 )
536+
537+ self ._check_percentile (array_3d , 1 , 50 , expected_result ,
538+ fast_percentile_method = True )
539+
506540 def test_percentile_3d_axis_two (self ):
507541 array_3d = np .arange (24 , dtype = np .int32 ).reshape ((2 , 3 , 4 ))
508542 expected_result = np .array ([[1.5 , 5.5 , 9.5 ],
@@ -520,6 +554,16 @@ def test_fast_percentile_3d_axis_two(self):
520554 self ._check_percentile (array_3d , 2 , 50 , expected_result ,
521555 fast_percentile_method = True )
522556
557+ def test_fast_percentile_3d_axis_two_masked_type_no_mask (self ):
558+ array_3d = np .arange (24 , dtype = np .int32 ).reshape ((2 , 3 , 4 ))
559+ array_3d = ma .MaskedArray (array_3d )
560+ expected_result = ma .MaskedArray ([[1.5 , 5.5 , 9.5 ],
561+ [13.5 , 17.5 , 21.5 ]],
562+ dtype = np .float32 )
563+
564+ self ._check_percentile (array_3d , 2 , 50 , expected_result ,
565+ fast_percentile_method = True )
566+
523567 def test_percentile_3d_masked (self ):
524568 cube = tests .stock .simple_3d_mask ()
525569 expected_result = [[12. , 13. , 14. , 15. ],
@@ -530,7 +574,7 @@ def test_percentile_3d_masked(self):
530574 cube , 75 , 'wibble' , expected_result ,
531575 CML_filename = 'last_quartile_foo_3d_masked.cml' )
532576
533- def test_fast_percentile_3d_masked (self ):
577+ def test_fast_percentile_3d_masked_type_masked (self ):
534578 cube = tests .stock .simple_3d_mask ()
535579 msg = 'Cannot use fast np.percentile method with masked array.'
536580
0 commit comments