1+ import math
2+
13import numpy
24import pytest
35from dpctl .tensor ._numpy_helper import AxisError
1214
1315
1416class TestMedian :
17+
1518 @testing .for_all_dtypes ()
1619 @testing .numpy_cupy_allclose (type_check = has_support_aspect64 ())
1720 def test_median_noaxis (self , xp , dtype ):
@@ -61,7 +64,14 @@ def test_median_invalid_axis(self):
6164 return xp .median (a , (- a .ndim - 1 , 1 ), keepdims = False )
6265
6366 with pytest .raises (AxisError ):
64- return xp .median (a , (0 , a .ndim ), keepdims = False )
67+ return xp .median (
68+ a ,
69+ (
70+ 0 ,
71+ a .ndim ,
72+ ),
73+ keepdims = False ,
74+ )
6575
6676 @testing .for_dtypes ("efdFD" )
6777 @testing .numpy_cupy_allclose ()
@@ -83,14 +93,64 @@ def test_median_nan(self, xp, dtype):
8393 )
8494)
8595class TestMedianAxis :
96+
8697 @testing .for_all_dtypes ()
8798 @testing .numpy_cupy_allclose (type_check = has_support_aspect64 ())
8899 def test_median_axis_sequence (self , xp , dtype ):
89100 a = testing .shaped_random (self .shape , xp , dtype )
90101 return xp .median (a , self .axis , keepdims = self .keepdims )
91102
92103
104+ @pytest .mark .skip ("dpnp.nanmedian() is not implemented yet" )
105+ @testing .parameterize (
106+ * testing .product (
107+ {
108+ "shape" : [(3 , 4 , 5 )],
109+ "axis" : [None , 0 , 1 , - 1 , (0 , 1 ), (0 , 2 ), (- 1 , - 2 ), [0 , 1 ]],
110+ "keepdims" : [True , False ],
111+ "overwrite_input" : [True , False ],
112+ }
113+ )
114+ )
115+ class TestNanMedian :
116+
117+ zero_density = 0.25
118+
119+ def _make_array (self , dtype ):
120+ dtype = numpy .dtype (dtype )
121+ if dtype .char in "efdFD" :
122+ r_dtype = dtype .char .lower ()
123+ a = testing .shaped_random (self .shape , numpy , dtype = r_dtype , scale = 1 )
124+ if dtype .char in "FD" :
125+ ai = a
126+ aj = testing .shaped_random (
127+ self .shape , numpy , dtype = r_dtype , scale = 1
128+ )
129+ ai [ai < math .sqrt (self .zero_density )] = 0
130+ aj [aj < math .sqrt (self .zero_density )] = 0
131+ a = ai + 1j * aj
132+ else :
133+ a [a < self .zero_density ] = 0
134+ a = a / a
135+ else :
136+ a = testing .shaped_random (self .shape , numpy , dtype = dtype )
137+ return a
138+
139+ @testing .for_all_dtypes ()
140+ @testing .numpy_cupy_allclose ()
141+ def test_nanmedian (self , xp , dtype ):
142+ a = xp .array (self ._make_array (dtype ))
143+ out = xp .nanmedian (
144+ a ,
145+ self .axis ,
146+ keepdims = self .keepdims ,
147+ overwrite_input = self .overwrite_input ,
148+ )
149+ return xp .ascontiguousarray (out )
150+
151+
93152class TestAverage :
153+
94154 _multiprocess_can_split_ = True
95155
96156 @testing .for_all_dtypes ()
@@ -115,7 +175,7 @@ def test_average_weights(self, xp, dtype):
115175 @testing .for_all_dtypes ()
116176 @testing .numpy_cupy_allclose (rtol = 2e-7 , type_check = has_support_aspect64 ())
117177 @pytest .mark .parametrize (
118- "axis, weights" , [(1 , False ), (None , True ), (1 , True )]
178+ "axis,weights" , [(1 , False ), (None , True ), (1 , True )]
119179 )
120180 def test_returned (self , xp , dtype , axis , weights ):
121181 a = testing .shaped_arange ((2 , 3 ), xp , dtype )
@@ -147,6 +207,7 @@ def test_average_keepdims_noaxis(self, xp, dtype, returned):
147207
148208
149209class TestMeanVar :
210+
150211 @testing .for_all_dtypes ()
151212 @testing .numpy_cupy_allclose (type_check = has_support_aspect64 ())
152213 def test_mean_all (self , xp , dtype ):
@@ -296,13 +357,14 @@ def test_external_std_axis_ddof(self, xp, dtype):
296357 )
297358)
298359class TestNanMean :
360+
299361 @testing .for_all_dtypes (no_float16 = True )
300362 @testing .numpy_cupy_allclose (rtol = 1e-6 , type_check = has_support_aspect64 ())
301363 def test_nanmean_without_nan (self , xp , dtype ):
302364 a = testing .shaped_random (self .shape , xp , dtype )
303365 return xp .nanmean (a , axis = self .axis , keepdims = self .keepdims )
304366
305- @pytest . mark . usefixtures ( "suppress_mean_empty_slice_numpy_warnings" )
367+ @ignore_runtime_warnings
306368 @testing .for_all_dtypes (no_float16 = True )
307369 @testing .numpy_cupy_allclose (rtol = 1e-6 , type_check = has_support_aspect64 ())
308370 def test_nanmean_with_nan_float (self , xp , dtype ):
@@ -316,17 +378,13 @@ def test_nanmean_with_nan_float(self, xp, dtype):
316378
317379
318380class TestNanMeanAdditional :
319- @pytest .mark .usefixtures ("suppress_mean_empty_slice_numpy_warnings" )
381+
382+ @ignore_runtime_warnings
320383 @testing .for_all_dtypes (no_float16 = True )
321- @testing .numpy_cupy_allclose (rtol = 1e-6 , type_check = has_support_aspect64 () )
384+ @testing .numpy_cupy_allclose (rtol = 1e-6 )
322385 def test_nanmean_out (self , xp , dtype ):
323386 a = testing .shaped_random ((10 , 20 , 30 ), xp , dtype )
324- # `numpy.mean` allows ``unsafe`` casting while `dpnp.mean` does not.
325- # So, output data type cannot be the same as input.
326- out_dtype = (
327- cupy .default_float_type (a .device ) if xp == cupy else numpy .float64
328- )
329- z = xp .zeros ((20 , 30 ), dtype = out_dtype )
387+ z = xp .zeros ((20 , 30 ), dtype = dtype )
330388
331389 if a .dtype .kind not in "biu" :
332390 a [1 , :] = xp .nan
@@ -355,7 +413,7 @@ def test_nanmean_float16(self, xp):
355413 a [0 ][0 ] = xp .nan
356414 return xp .nanmean (a )
357415
358- @pytest . mark . usefixtures ( "suppress_mean_empty_slice_numpy_warnings" )
416+ @ignore_runtime_warnings
359417 @testing .numpy_cupy_allclose (rtol = 1e-6 , type_check = has_support_aspect64 ())
360418 def test_nanmean_all_nan (self , xp ):
361419 a = xp .zeros ((3 , 4 ))
@@ -374,7 +432,8 @@ def test_nanmean_all_nan(self, xp):
374432 )
375433)
376434class TestNanVarStd :
377- @pytest .mark .usefixtures ("suppress_dof_numpy_warnings" )
435+
436+ @ignore_runtime_warnings
378437 @testing .for_all_dtypes (no_float16 = True )
379438 @testing .numpy_cupy_allclose (rtol = 1e-6 , type_check = has_support_aspect64 ())
380439 def test_nanvar (self , xp , dtype ):
@@ -385,7 +444,7 @@ def test_nanvar(self, xp, dtype):
385444 a , axis = self .axis , ddof = self .ddof , keepdims = self .keepdims
386445 )
387446
388- @pytest . mark . usefixtures ( "suppress_dof_numpy_warnings" )
447+ @ignore_runtime_warnings
389448 @testing .for_all_dtypes (no_float16 = True )
390449 @testing .numpy_cupy_allclose (rtol = 1e-6 , type_check = has_support_aspect64 ())
391450 def test_nanstd (self , xp , dtype ):
@@ -398,7 +457,8 @@ def test_nanstd(self, xp, dtype):
398457
399458
400459class TestNanVarStdAdditional :
401- @pytest .mark .usefixtures ("suppress_dof_numpy_warnings" )
460+
461+ @ignore_runtime_warnings
402462 @testing .for_all_dtypes (no_float16 = True )
403463 @testing .numpy_cupy_allclose (rtol = 1e-6 , type_check = has_support_aspect64 ())
404464 def test_nanvar_out (self , xp , dtype ):
@@ -432,7 +492,7 @@ def test_nanvar_float16(self, xp):
432492 a [0 ][0 ] = xp .nan
433493 return xp .nanvar (a , axis = 0 )
434494
435- @pytest . mark . usefixtures ( "suppress_dof_numpy_warnings" )
495+ @ignore_runtime_warnings
436496 @testing .for_all_dtypes (no_float16 = True )
437497 @testing .numpy_cupy_allclose (rtol = 1e-6 , type_check = has_support_aspect64 ())
438498 def test_nanstd_out (self , xp , dtype ):
@@ -488,6 +548,7 @@ def test_nanstd_float16(self, xp):
488548 "suppress_mean_empty_slice_numpy_warnings" ,
489549)
490550class TestProductZeroLength :
551+
491552 @testing .for_all_dtypes (no_complex = True )
492553 @testing .numpy_cupy_allclose (type_check = has_support_aspect64 ())
493554 def test_external_mean_zero_len (self , xp , dtype ):
0 commit comments