def initialize_wavefront_from_steps(cls, x_start=0.0, x_step=0.0, y_start=0.0, y_step=0.0, number_of_points=(100, 100), wavelength=1e-10, polarization=Polarization.SIGMA): sM = ScaledMatrix.initialize_from_steps(numpy.full(number_of_points, (1.0 + 0.0j), dtype=complex), x_start, x_step, y_start, y_step, interpolator=False) if ((polarization == Polarization.PI) or (polarization == Polarization.TOTAL)): sM_pi = ScaledMatrix.initialize_from_steps(numpy.full( number_of_points, (0.0 + 0.0j), dtype=complex), x_start, x_step, y_start, y_step, interpolator=False) else: sM_pi = None return GenericWavefront2D(wavelength, sM, sM_pi)
def initialize_wavefront_from_arrays( cls, x_array, y_array, z_array, z_pi_array=None, wavelength=1e-10, ): sh = z_array.shape if sh[0] != x_array.size: raise Exception("Unmatched shapes for x") if sh[1] != y_array.size: raise Exception("Unmatched shapes for y") sM = ScaledMatrix.initialize_from_steps( z_array, x_array[0], numpy.abs(x_array[1] - x_array[0]), y_array[0], numpy.abs(y_array[1] - y_array[0]), interpolator=False) if z_pi_array is not None: sMpi = ScaledMatrix.initialize_from_steps( z_pi_array, x_array[0], numpy.abs(x_array[1] - x_array[0]), y_array[0], numpy.abs(y_array[1] - y_array[0]), interpolator=False) else: sMpi = None return Wavefront2D(wavelength, sM, sMpi)
def initialize_wavefront_from_arrays(cls,x_array, y_array, z_array, z_array_pi=None, wavelength=1e-10): sh = z_array.shape if sh[0] != x_array.size: raise Exception("Unmatched shapes for x") if sh[1] != y_array.size: raise Exception("Unmatched shapes for y") sM = ScaledMatrix.initialize_from_steps( z_array,x_array[0],numpy.abs(x_array[1]-x_array[0]), y_array[0],numpy.abs(y_array[1]-y_array[0]),interpolator=False) if z_array_pi is None: sM_pi = None else: sh = z_array_pi.shape if sh[0] != x_array.size: raise Exception("Unmatched shapes for x (Pi Polarization)") if sh[1] != y_array.size: raise Exception("Unmatched shapes for y (Pi Polarization)") sM_pi = ScaledMatrix.initialize_from_steps( z_array_pi,x_array[0],numpy.abs(x_array[1]-x_array[0]), y_array[0],numpy.abs(y_array[1]-y_array[0]),interpolator=False) return GenericWavefront2D(wavelength, sM, sM_pi)
def initialize_wavefront_from_steps(cls, x_start=0.0, x_step=0.0, y_start=0.0, y_step=0.0, number_of_points=(100,100),wavelength=1e-10, ): sM = ScaledMatrix.initialize_from_steps( numpy.full(number_of_points,(1.0 + 0.0j), dtype=complex), x_start,x_step,y_start,y_step,interpolator=False) return Wavefront2D(wavelength,sM)
def initialize_wavefront_from_arrays(cls,x_array, y_array, z_array, wavelength=1e-10): sh = z_array.shape if sh[0] != x_array.size: raise Exception("Unmatched shapes for x") if sh[1] != y_array.size: raise Exception("Unmatched shapes for y") sM = ScaledMatrix.initialize_from_steps( z_array,x_array[0],numpy.abs(x_array[1]-x_array[0]), y_array[0],numpy.abs(y_array[1]-y_array[0]),interpolator=False) return Wavefront2D(wavelength,sM)
def test_ScaledMatrix(self,do_plot=do_plot): # # Matrix # x = numpy.arange(10,20,2.0) y = numpy.arange(20,25,1.0) xy = numpy.meshgrid(x,y) X = xy[0].T Y = xy[1].T Z = Y print("x,y,X,Y,Z shapes: ",x.shape,y.shape,X.shape,Y.shape,Z.shape) print("x,y,X,Y,Z shapes: ",x.shape,y.shape,X.shape,Y.shape,Z.shape) # # scaledMatrix.initialize + set_scale_from_steps # print("\nTesting ScaledMatrix.initialize + set_scale_from_steps...") scaled_matrix = ScaledMatrix.initialize(Z) print(" Matrix shape", scaled_matrix.shape()) scaled_matrix.set_scale_from_steps(0,x[0],numpy.abs(x[1]-x[0])) scaled_matrix.set_scale_from_steps(1,y[0],numpy.abs(y[1]-y[0])) print("Original x: ",x) print("Stored x: ",scaled_matrix.get_x_values()) print("Original y: ",y) print("Stored y: ",scaled_matrix.get_y_values()) numpy.testing.assert_equal(x,scaled_matrix.get_x_values()) numpy.testing.assert_equal(y,scaled_matrix.get_y_values()) print(" Matrix X value x=0 : ", scaled_matrix.get_x_value(0.0)) print(" Matrix Y value x_index=3 is %g (to compare with %g) "%(scaled_matrix.get_x_value(2.0),x[0]+2*(x[1]-x[0]))) print(" Matrix Y value y_index=3 is %g (to compare with %g) "%(scaled_matrix.get_y_value(2.0),y[0]+2*(y[1]-y[0]))) self.assertAlmostEqual(scaled_matrix.get_x_value(2.0),x[0]+2*(x[1]-x[0]),10) self.assertAlmostEqual(scaled_matrix.get_y_value(2.0),y[0]+2*(y[1]-y[0]),10) # # scaledMatrix.initialize + set_scale_from_range # x = numpy.linspace(-10,10,10) y = numpy.linspace(-25,25,20) xy = numpy.meshgrid(x,y) X = xy[0].T Y = xy[1].T Z = Y print("\nTesting ScaledMatrix.initialize + set_scale_from_range...") scaled_matrix = ScaledMatrix.initialize(Z) # Z[0] contains Y print(" Matrix shape", scaled_matrix.shape()) scaled_matrix.set_scale_from_range(0,x[0],x[-1]) scaled_matrix.set_scale_from_range(1,y[0],y[-1]) print("Original x: ",x) print("Stored x: ",scaled_matrix.get_x_values()) print("Original y: ",y) print("Stored y: ",scaled_matrix.get_y_values()) numpy.testing.assert_almost_equal(x,scaled_matrix.get_x_values(),11) numpy.testing.assert_almost_equal(y,scaled_matrix.get_y_values(),11) print(" Matrix X value x=0 : ", scaled_matrix.get_x_value(0.0)) print(" Matrix Y value x_index=5 is %g (to compare with %g) "%(scaled_matrix.get_x_value(5.0),x[0]+5*(x[1]-x[0]))) print(" Matrix Y value y_index=5 is %g (to compare with %g) "%(scaled_matrix.get_y_value(5.0),y[0]+5*(y[1]-y[0]))) self.assertAlmostEqual(scaled_matrix.get_x_value(5.0),x[0]+5*(x[1]-x[0]),10) self.assertAlmostEqual(scaled_matrix.get_y_value(5.0),y[0]+5*(y[1]-y[0]),10) # # scaledMatrix.initialize_from_steps # x = numpy.arange(10,20,0.2) y = numpy.arange(20,25,0.1) xy = numpy.meshgrid(x,y) X = xy[0].T Y = xy[1].T Z = Y print("\nTesting ScaledMatrix.initialize_from_steps...") scaled_matrix2 = ScaledMatrix.initialize_from_steps(Z,x[0],numpy.abs(x[1]-x[0]),y[0],numpy.abs(y[1]-y[0])) print(" Matrix 2 shape", scaled_matrix2.shape()) print(" Matrix 2 value x=0 : ", scaled_matrix2.get_x_value(0.0)) print(" Matrix 2 value x=5 is %g (to compare with %g) "%(scaled_matrix2.get_x_value(5.0),x[0]+5*(x[1]-x[0]))) self.assertAlmostEqual(scaled_matrix2.get_x_value(5.0),x[0]+5*(x[1]-x[0])) print(" Matrix 2 value y=5 is %g (to compare with %g) "%(scaled_matrix2.get_y_value(5.0),y[0]+5*(y[1]-y[0]))) self.assertAlmostEqual(scaled_matrix2.get_y_value(5.0),y[0]+5*(y[1]-y[0])) # # scaledMatrix.initialize_from_range # print("\nTesting ScaledMatrix.initialize_from_range...") x = numpy.linspace(-10,10,20) y = numpy.linspace(-25,25,30) xy = numpy.meshgrid(x,y) X = xy[0].T Y = xy[1].T Z = X*0+(3+2j) # scaled_matrix3 = ScaledMatrix.initialize_from_range(Z,x[0],x[-1],y[0],y[-1]) print("Matrix 3 shape", scaled_matrix3.shape()) print("Matrix 3 value x=0 : ", scaled_matrix3.get_x_value(0.0)) print("Matrix 3 value x=15 is %g (to compare with %g) "%(scaled_matrix3.get_x_value(15.0),x[0]+15*(x[1]-x[0]))) self.assertAlmostEqual(scaled_matrix3.get_x_value(15.0),x[0]+15*(x[1]-x[0])) print("Matrix 3 value y=15 is %g (to compare with %g) "%(scaled_matrix3.get_y_value(15.0),y[0]+15*(y[1]-y[0]))) self.assertAlmostEqual(scaled_matrix3.get_y_value(15.0),y[0]+15*(y[1]-y[0])) # print("Matrix 3 X : ", scaled_matrix3.get_x_values()) # print("Matrix 3 Y : ", scaled_matrix3.get_y_values()) # print("Matrix 3 Z : ", scaled_matrix3.get_z_values()) # # interpolator # print("\nTesting interpolator on the same grid...") # constant Z = X * Y scaled_matrix4 = ScaledMatrix.initialize_from_range(Z,x[0],x[-1],y[0],y[-1]) scaled_matrix4.compute_interpolator() print("Matrix 4 interpolation x=110,y=210 is ",scaled_matrix4.interpolate_value(110,210)) # interpolate in the same grid s4int = scaled_matrix4.interpolate_value(X,Y) print("Matrix 4 interpolation same grid (shape)",s4int.shape," before: ",scaled_matrix4.shape()) numpy.testing.assert_almost_equal(scaled_matrix4.get_z_values(),s4int,3) print("\nTesting interpolator on a grid with less points...") # interpolate in a grid with much less points xrebin = numpy.linspace(x[0],x[-1],x.size/10) yrebin = numpy.linspace(y[0],y[-1],y.size/10) xyrebin =numpy.meshgrid( xrebin, yrebin) Xrebin = xyrebin[0].T Yrebin = xyrebin[1].T s4rebin = scaled_matrix4.interpolate_value(Xrebin,Yrebin) if do_plot == 1: from srxraylib.plot.gol import plot_image plot_image(scaled_matrix4.get_z_values(),scaled_matrix4.get_x_values(),scaled_matrix4.get_y_values(), title="Original",show=0) plot_image(numpy.real(s4int),scaled_matrix4.get_x_values(),scaled_matrix4.get_y_values(), title="Interpolated on same grid",show=0) plot_image(numpy.real(s4rebin),xrebin,yrebin, title="Interpolated on a grid with 10 times less points") for xi in xrebin: for yi in yrebin: yint = scaled_matrix4.interpolate_value(xi,yi) print(" Interpolated at (%g,%g) is %g+%gi (expected %g)"%(xi,yi,yint.real,yint.imag,xi*yi)) self.assertAlmostEqual(scaled_matrix4.interpolate_value(xi,yi),xi*yi)