@@ -335,14 +335,34 @@ def test_axis_2(self):
335335 def test_inconsistent_shape (self ):
336336 data = np .empty ([5 , 4 , 23 , 7 , 3 ])
337337 zdata = np .empty ([5 , 4 , 3 , 7 , 3 ])
338- with self .assertRaises (ValueError ):
338+ emsg = 'z_src .* is not a subset of fz_src'
339+ with self .assertRaisesRegexp (ValueError , emsg ):
339340 vinterp ._Interpolation ([1 , 3 ], data , zdata , axis = 2 )
340341
341342 def test_axis_out_of_bounds (self ):
342343 data = np .empty ([5 , 4 ])
343344 zdata = np .empty ([5 , 4 ])
344- with self .assertRaises (ValueError ):
345- vinterp ._Interpolation ([1 , 3 ], data , zdata , axis = 4 )
345+ axis = 4
346+ emsg = 'Axis {} out of range'
347+ with self .assertRaisesRegexp (ValueError , emsg .format (axis )):
348+ vinterp ._Interpolation ([1 , 3 ], data , zdata , axis = axis )
349+
350+ def test_nd_inconsistent_ndims (self ):
351+ z_target = np .empty ((2 , 3 , 4 ))
352+ z_src = np .empty ((3 , 4 ))
353+ fz_src = np .empty ((2 , 3 , 4 ))
354+ emsg = 'z_target and z_src must have the same number of dimensions'
355+ with self .assertRaisesRegexp (ValueError , emsg ):
356+ vinterp ._Interpolation (z_target , z_src , fz_src )
357+
358+ def test_nd_inconsistent_shape (self ):
359+ z_target = np .empty ((3 , 2 , 6 ))
360+ z_src = np .empty ((3 , 4 , 5 ))
361+ fz_src = np .empty ((2 , 3 , 4 , 5 ))
362+ emsg = ('z_target and z_src have different shapes, '
363+ 'got \(3, :, 6\) != \(3, :, 5\)' )
364+ with self .assertRaisesRegexp (ValueError , emsg ):
365+ vinterp ._Interpolation (z_target , z_src , fz_src , axis = 2 )
346366
347367 def test_result_dtype_f4 (self ):
348368 interp = vinterp ._Interpolation ([17.5 ], np .arange (4 ) * 10 ,
@@ -362,26 +382,79 @@ def test_result_dtype_f8(self):
362382
363383
364384class Test__Interpolation_interpolate_z_target_nd (unittest .TestCase ):
365- def test_target_z_3d_axis_0 (self ):
385+ def test_target_z_3d_on_axis_0 (self ):
366386 z_target = z_source = f_source = np .arange (3 ) * np .ones ([4 , 2 , 3 ])
367387 interp = vinterp ._Interpolation (z_target , z_source , f_source ,
368388 axis = 0 , extrapolation = stratify .EXTRAPOLATE_NEAREST )
369389 result = interp .interpolate_z_target_nd ()
370390 assert_array_equal (result , f_source )
371391
372- def test_target_z_3d_axis_m1 (self ):
392+ def test_target_z_3d_on_axis_m1 (self ):
373393 z_target = z_source = f_source = np .arange (3 ) * np .ones ([4 , 2 , 3 ])
374394 interp = vinterp ._Interpolation (z_target , z_source , f_source ,
375395 axis = - 1 , extrapolation = stratify .EXTRAPOLATE_NEAREST )
376396 result = interp .interpolate_z_target_nd ()
377397 assert_array_equal (result , f_source )
378398
399+ def test_target_z_2d_over_3d_on_axis_1 (self ):
400+ """
401+ Test the case where z_target(2, 4) and z_src(3, 4) are 2d, but the
402+ source data fz_src(3, 3, 4) is 3d. z_target and z_src cover the last
403+ 2 dimensions of fz_src. The axis of interpolation is axis=1 wrt fz_src.
404+
405+ """
406+ # Generate the 3d source data fz_src(3, 3, 4)
407+ base = np .arange (3 ).reshape (1 , 3 , 1 ) * 2
408+ data = np .broadcast_to (base , (3 , 3 , 4 ))
409+ fz_src = data * np .arange (1 , 4 ).reshape (3 , 1 , 1 ) * 10
410+ # Generate the 2d target coordinate z_target(2, 4)
411+ # The target coordinate is configured to request the interpolated
412+ # mid-points over axis=1 of fz_src.
413+ z_target = np .repeat (np .arange (1 , 4 , 2 ).reshape (2 , 1 ), 4 , axis = 1 ) * 10
414+ # Generate the 2d source coordinate z_src(3, 4)
415+ z_src = np .repeat (np .arange (3 ).reshape (3 , 1 ), 4 , axis = 1 ) * 20
416+ # Configure the vertical interpolator.
417+ interp = vinterp ._Interpolation (z_target , z_src , fz_src , axis = 1 )
418+ # Perform the vertical interpolation.
419+ result = interp .interpolate_z_target_nd ()
420+ # Generate the 3d expected interpolated result(3, 2, 4).
421+ expected = np .repeat (z_target [np .newaxis , ...], 3 , axis = 0 )
422+ expected = expected * np .arange (1 , 4 ).reshape (3 , 1 , 1 )
423+ assert_array_equal (result , expected )
424+
425+ def test_target_z_2d_over_3d_on_axis_m1 (self ):
426+ """
427+ Test the case where z_target(3, 3) and z_src(3, 4) are 2d, but the
428+ source data fz_src(3, 3, 4) is 3d. z_target and z_src cover the last
429+ 2 dimensions of fz_src. The axis of interpolation is the default last
430+ dimension, axis=-1, wrt fx_src.
431+
432+ """
433+ # Generate the 3d source data fz_src(3, 3, 4)
434+ base = np .arange (4 ) * 2
435+ data = np .broadcast_to (base , (3 , 3 , 4 ))
436+ fz_src = data * np .arange (1 , 4 ).reshape (3 , 1 , 1 ) * 10
437+ # Generate the 2d target coordinate z_target(3, 3)
438+ # The target coordinate is configured to request the interpolated
439+ # mid-points over axis=-1 (aka axis=2) of fz_src.
440+ z_target = np .repeat (np .arange (1 , 6 , 2 ).reshape (1 , 3 ), 3 , axis = 0 ) * 10
441+ # Generate the 2d source coordinate z_src(3, 4)
442+ z_src = np .repeat (np .arange (4 ).reshape (1 , 4 ), 3 , axis = 0 ) * 20
443+ # Configure the vertical interpolator.
444+ interp = vinterp ._Interpolation (z_target , z_src , fz_src ,)
445+ # Perform the vertical interpolation.
446+ result = interp .interpolate_z_target_nd ()
447+ # Generate the 3d expected interpolated result(3, 3, 3)
448+ expected = np .repeat (z_target [np .newaxis , ...], 3 , axis = 0 )
449+ expected = expected * np .arange (1 , 4 ).reshape (3 , 1 , 1 )
450+ assert_array_equal (result , expected )
451+
379452
380453class Test_interpolate (unittest .TestCase ):
381454 def test_target_z_3d_axis_0 (self ):
382455 z_target = z_source = f_source = np .arange (3 ) * np .ones ([4 , 2 , 3 ])
383- result = vinterp .interpolate (z_target , z_source , f_source ,
384- extrapolation = 'linear' )
456+ result = vinterp .interpolate (z_target , z_source , f_source ,
457+ extrapolation = 'linear' )
385458 assert_array_equal (result , f_source )
386459
387460
0 commit comments