10 run_test = xboa.test.TestTools.run_test
11 run_test_group = xboa.test.TestTools.run_test_group
12 __float_tol = xboa.test.TestTools.__float_tol
13 parse_tests = xboa.test.TestTools.parse_tests
14 __test_root_hist = xboa.test.TestTools.test_root_hist
15 __test_root_canvas = xboa.test.TestTools.test_root_canvas
16 __test_root_graph = xboa.test.TestTools.test_root_graph
27 from numpy
import linalg
31 Common.has_multiprocessing()
32 import multiprocessing
44 out1 = open(
'test1.in',
'w')
45 if not out1:
return 'warn'
46 out1.write(
'some test input\nis written here')
48 Common.substitute(
'test1.in',
'test1.out', {
'some':
'a bit of',
'test':
'tested',
'input':
'output',
'written':
'read',
'there':
'here',
'kx':
'uga'})
49 out2 = open(
'test1.out',
'r')
50 if not out2:
return 'warn'
51 lines = out2.readlines()
53 if not lines == [
'a bit of tested output\n',
'is read here']:
return 'fail'
55 os.remove(
'test1.out')
67 y.append(math.sin(value))
72 solution = Common.nd_newton_raphson1(__sin_x, [1., 1.e-5], [0.1, 0.1], [1.e-3,1.e-3])
74 if abs(y[0]) > 1e-5
or abs(y[1] > 1e-5):
79 solution = Common.nd_newton_raphson2(__sin_x_one_pass, [1.e-5, 1.e-5], [0.1, 0.1], [1.e-3,1.e-3])
81 if abs(y[0]) > 1e-5
or abs(y[1] > 1e-5):
109 canvas = Common.make_root_canvas(
'canvas_name')
110 testpass &= canvas.GetName().find(
'canvas_name') > -1
111 canvas = Common.make_root_canvas(
'canvas_name_2',
'canvas_title', bg_color=10, highlight_color=12, border_mode=1, frame_fill_color=2)
113 if testpass:
return 'pass'
119 testpass &= Common.n_bins(n_p, n_dimensions=1 ) == (n_p/10+1, 0, 0)
120 testpass &= Common.n_bins(n_p, n_dimensions=2) == (int(n_p**0.7/10.)+1, int(n_p**0.7/10.)+1, 0)
121 testpass &= Common.n_bins(n_p, n_dimensions=3) == (int(n_p**0.5/10.)+1, int(n_p**0.5/10.)+1, int(n_p**0.5/10.)+1)
122 testpass &= Common.n_bins(n_p, nx_bins=10, n_dimensions=3) == (10, int(n_p**0.5/10.)+1, int(n_p**0.5/10.)+1)
123 testpass &= Common.n_bins(n_p, ny_bins=10, n_dimensions=3) == (int(n_p**0.5/10.)+1, 10, int(n_p**0.5/10.)+1)
124 testpass &= Common.n_bins(n_p, nz_bins=10, n_dimensions=3) == (int(n_p**0.5/10.)+1, int(n_p**0.5/10.)+1, 10)
125 if testpass:
return 'pass'
131 y.append(const1*math.sin(const2*x))
136 out = Common.min_max([-0.1,-0.5,-0.9,0.0])
137 test_pass = test_pass
and abs(out[0]--0.99) < __float_tol
138 test_pass = test_pass
and abs(out[1]-+0.09) < __float_tol
139 out = Common.min_max(x_float_list=[-0.1,-0.5,-0.9,0.0], weight_list=[1,1,0,1])
140 test_pass = test_pass
and abs(out[0]--0.55) < __float_tol
141 test_pass = test_pass
and abs(out[1]-+0.05) < __float_tol
142 out = Common.min_max(x_float_list=[-0.1,-0.5,-0.9,0.0], weight_list=[1,1,0,1], margin=0.0)
143 test_pass = test_pass
and abs(out[0]--0.5) < __float_tol
144 test_pass = test_pass
and abs(out[1]-+0.0) < __float_tol
145 out = Common.min_max(x_float_list=[-0.1,-0.5,-0.9,0.0], weight_list=[1,1,0,1], margin=0.0, xmin=+100.)
146 test_pass = test_pass
and abs(out[0]-100.) < __float_tol
147 test_pass = test_pass
and abs(out[1]-+0.0) < __float_tol
148 out = Common.min_max(x_float_list=[-0.1,-0.5,-0.9,0.0], weight_list=[1,1,0,1], margin=0.0, xmax=-100.)
149 test_pass = test_pass
and abs(out[0]--0.5) < __float_tol
150 test_pass = test_pass
and abs(out[1]+100.) < __float_tol
151 out = Common.min_max([-10., 10.], [0.,0.])
152 test_pass = test_pass
and abs(out[0]+1.) < __float_tol
153 test_pass = test_pass
and abs(out[1]-1.) < __float_tol
154 if test_pass:
return 'pass'
158 multilist = [[-1,1,0,7,4],[
'a',
'c',
'b',
'e',
'd'],[
'alfred',
'caltrops',
'bracken',
'elven',
'daphne']]
159 multilist = Common.multisort(multilist)
161 if multilist == [[-1,0,1,4,7],[
'a',
'b',
'c',
'd',
'e'],[
'alfred',
'bracken',
'caltrops',
'daphne',
'elven']]:
return 'pass'
166 for i
in range(len(n)): n[i] = float(n[i])**2
167 edges = Common.get_bin_edges(n, 20)
168 if len(edges) != 21:
return 'fail'
169 if edges[0] != 0.
or edges[-1] != n[-1]:
return 'fail'
170 for i
in range( len(edges) - 1):
171 if abs(edges[i+1] - edges[i] - n[-1]/20.) > __float_tol:
return 'fail'
173 edges = Common.get_bin_edges(n, 20, -10, 10)
174 for i,x
in enumerate(edges):
175 if abs(x-float(i)+10.) > __float_tol:
return 'fail'
180 x_list = range(-200,10000)
181 y_list = range(-100,10100)
183 for i
in range( len(x_list) ):
184 x_list[i] = float(x_list[i])/10.
185 y_list[i] = float(y_list[i])/20.
186 w_list.append(x_list[i])
187 bin_x = range(-15,100,10)
188 bin_y = range(-4, 200,10)
190 c_1d = numpy.zeros((len(bin_x)-1,1))
191 c_2d = numpy.zeros((len(bin_x)-1,len(bin_y)-1))
192 c_2d_w = numpy.zeros((len(bin_x)-1,len(bin_y)-1))
194 for i,x
in enumerate(x_list):
195 p = bisect.bisect_right(bin_x,x_list[i])-1
196 q = bisect.bisect_right(bin_y,y_list[i])-1
197 if p>=0
and p<len(c_1d):
199 if q>=0
and q<len(c_2d[0]):
201 c_2d_w[p][q] += w_list[i]
203 return ( (c_1d, c_2d, c_2d_w), (bin_x, bin_y), (x_list,y_list,w_list))
210 hist_1d = Common.histogram(x_list, bin_x)
211 hist_2d = Common.histogram(x_list, bin_x, y_list, bin_y)
212 hist_2d_w = Common.histogram(x_list, bin_x, y_list, bin_y, w_list)
214 testpass &= hist_1d [1] == bin_x
215 testpass &= hist_2d [1] == bin_x
and hist_2d [2] == bin_y
216 testpass &= hist_2d_w[1] == bin_x
and hist_2d_w[2] == bin_y
219 for p
in range( pm ):
220 testpass &= abs(hist_1d[0][p,0] - c_1d[p,0]) < __float_tol
221 for q
in range( qm ):
222 testpass &= abs(hist_2d [0][p,q] - c_2d [p,q]) < __float_tol
223 testpass &= abs(hist_2d_w[0][p,q] - c_2d_w[p,q]) < __float_tol
228 x_list = range(0,10000)
230 for i
in range( len(x_list) ):
231 x_list[i] = float(x_list[i])/10000.
232 w_list.append(x_list[i])
234 hist1 = Common.make_root_histogram(
'hist sin(x)', y_list,
'hist of x', 100)
235 hist2 = Common.make_root_histogram(
'hist sin(x)', y_list,
'hist of x', 50, x_list,
'hist of y', 50)
236 hist3 = Common.make_root_histogram(
'hist sin(x)', y_list,
'hist of x', 10, x_list,
'hist of y', 10, w_list)
237 testpass =
__test_root_hist(hist3,
'hist sin(x)',
'hist of x',
'hist of y',Common.min_max(y_list, w_list, rg.histo_margin)[0], Common.min_max(y_list, w_list, rg.histo_margin)[1],
238 Common.min_max(x_list, w_list, rg.histo_margin)[0], Common.min_max(x_list, w_list, rg.histo_margin)[1],
239 rg.line_color, rg.line_style, rg.line_width, rg.fill_color,
'')
240 hist4 = Common.make_root_histogram(
'hist sin(x)', y_list,
'hist of x', 10, x_list,
'hist of y', 10, w_list, xmin=y_list[7500],xmax=y_list[2500],ymin=x_list[2500],ymax=x_list[7500],
241 line_color=1, line_style=2, line_width=3, fill_color=4, hist_title_string=
'title_string')
242 testpass =
__test_root_hist(hist4,
'hist sin(x)',
'hist of x',
'hist of y', y_list[7500], y_list[2500], x_list[2500], x_list[7500], 1, 2, 3, 4,
'title_string')
243 canvas1 = Common.make_root_canvas(
'hist_test1')
245 canvas2 = Common.make_root_canvas(
'hist_test2')
247 canvas3 = Common.make_root_canvas(
'hist_test3')
249 canvas4 = Common.make_root_canvas(
'hist_test4')
251 if not testpass:
return 'fail'
256 canvas = Common.make_root_canvas(
'graph_test')
257 x_list = range(0,100)
258 for i
in range( len(x_list) ): x_list[i] = float(x_list[i])/100.
260 mm = Common.min_max(x_list) + Common.min_max(y_list)
261 (hist,graph) = Common.make_root_graph(
'test', x_list,
'x [2#pi rad]', y_list,
'a*sin(b*x)')
262 if not __test_root_hist(hist,
'',
'x [2#pi rad]',
'a*sin(b*x)', mm[0], mm[1], mm[2], mm[3], rg.line_color, rg.line_style, rg.line_width, rg.fill_color,
''):
return 'fail'
263 (hist,graph) = Common.make_root_graph(
'test', x_list,
'x [2#pi rad]', y_list,
'a*sin(b*x)', xmin=-1001., xmax=+1001, ymin=-1002., ymax=+1002.,line_color=201, line_style=202, line_width=203, fill_color=204, hist_title_string=
'mr title')
264 testpass &=
__test_root_hist(hist,
'namey',
'x [2#pi rad]',
'a*sin(b*x)', -1001., +1001., -1002., +1002., rg.line_color, rg.line_style, rg.line_width, rg.fill_color,
'mr title')
265 testpass &=
__test_root_graph(graph,
'namey', x_list, y_list, 201, 202, 203, 204)
266 (x_list, y_list) = ([3,2,1], [3,2,1])
267 (hist,graph) = Common.make_root_graph(
'test', x_list,
'x', y_list,
'y')
268 testpass &= (x_list, y_list) == ([3,2,1], [3,2,1])
272 if testpass:
return 'pass'
279 canvas = Common.make_root_canvas(
'multigraph_test')
283 x_list_of_lists.append(range(0,50))
284 for i
in range( len(x_list_of_lists[-1]) ):
285 x_list_of_lists[-1][i] /= 10.
286 x_list_of_lists.append(range(0,100))
287 for i
in range( len(x_list_of_lists[-1]) ):
288 x_list_of_lists[-1][i] /= 20.
289 for i
in range( len(x_list_of_lists) ):
290 y_list_of_lists.append(
__sine_list(i+1, i+1, x_list_of_lists[i]))
291 (hist,list_of_graphs) = Common.make_root_multigraph(
'test', x_list_of_lists,
'x [rad]', y_list_of_lists,
'a*sin(b*x)')
293 for graph
in list_of_graphs:
298 Common.wait_for_root()
306 Common.wait_for_matplot()
310 x_list = range(0,100)
311 for i
in range( len(x_list) ): x_list[i] = float(x_list[i])/100.
313 myplot = Common.make_matplot_graph(x_list,
'x [2#pi rad]', y_list,
'a*sin(b*x)')
317 x_list = range(0,10000)
319 for i
in range( len(x_list) ):
320 x_list[i] = float(x_list[i])/10000.
321 w_list.append(x_list[i])
323 Common.make_matplot_histogram(y_list,
'hist of x', 100)
324 Common.make_matplot_histogram(y_list,
'hist of x', 50, x_list,
'hist of y', 50)
325 Common.make_matplot_histogram(y_list,
'hist of x', 10, x_list,
'hist of y', 10, w_list)
329 x_list = range(0,10000)
330 for i
in range( len(x_list) ):
331 x_list[i] = float(x_list[i])/10000.
333 Common.make_matplot_scatter(y_list,
'hist of x', x_list,
'hist of y')
341 x_list_of_lists.append(range(0,50))
342 for i
in range( len(x_list_of_lists[-1]) ):
343 x_list_of_lists[-1][i] /= 10.
344 x_list_of_lists.append(range(0,100))
345 for i
in range( len(x_list_of_lists[-1]) ):
346 x_list_of_lists[-1][i] /= 20.
347 for i
in range( len(x_list_of_lists) ):
348 y_list_of_lists.append(
__sine_list(i+1, i+1, x_list_of_lists[i]))
349 Common.make_matplot_multigraph(x_list_of_lists,
'x [rad]', y_list_of_lists,
'a*sin(b*x)')
353 Common.matplot_show_and_continue()
357 if some_arg !=
'hello' and some_other_arg !=
'world':
358 print 'error in common_subprocess_test - aborting'
364 if some_arg !=
'hello' and some_other_arg !=
'world':
365 print 'error in common_subprocess_test - aborting'
375 pid1 = Common.subprocess(__subprocess_function_1, (
'hello',
'world'))
376 pid2 = Common.subprocess(__subprocess_function_2, (
'hello',
'world'))
384 for i
in range(10000): list_1.append( random.gauss(0., 1.) )
385 for i
in range(10000): list_2.append( random.gauss(0., 1.) )
386 for i
in range(10000): list_3.append( random.gauss(0.1, 1.1) )
388 testpass &= 1.-Common.kolmogorov_smirnov_test(list_1, list_1) < __float_tol
389 testpass &= Common.kolmogorov_smirnov_test(list_1, list_2) > 1e-3
390 testpass &= Common.kolmogorov_smirnov_test(list_1, list_3) < 1e-3
391 if testpass:
return 'pass'
396 if type(process) == type(1):
403 return process.is_alive()
406 pid1 = Common.subprocess(__subprocess_function_1, (
'hello',
'world'))
407 pid2 = Common.subprocess(__subprocess_function_2, (
'hello',
'world'))
408 Common.kill_all_subprocesses()
410 return 'fail - there is a known issue with python < 2.6 if using PyROOT'
416 for dim
in range(2,5):
417 for v_count
in range(10):
418 vector = numpy.matrix(numpy.zeros(dim))
419 for ind,value
in numpy.ndenumerate(vector):
420 vector[ind] = random.random()
421 matrix_inverse = numpy.matrix(numpy.ones([dim,dim]))
422 for (i,j),value
in numpy.ndenumerate(matrix_inverse):
423 if i != j: matrix_inverse[i,j] /= (i+1)/10./(j+1)
424 vector = Common.normalise_vector(vector, matrix_inverse)
425 if abs( (vector*matrix_inverse*vector.transpose())[0,0]-1. )>Common.float_tolerance:
430 grid_list = ((2, 2), (3,2), (2,3),(2,4))
432 for grid_var
in grid_list:
433 n_dim,n_per_dim = grid_var
434 grid = Common.make_grid(*grid_var)
436 testpass &= len(grid)==n_per_dim**n_dim
438 for i,vec1
in enumerate(grid):
439 for j,vec2
in enumerate(grid[i+1:]):
440 testpass &= ((vec1-vec2)*(vec1-vec2).transpose())[0,0] > Common.float_tolerance
444 grid_spacing = (1./float(n_per_dim-1))
445 testpass &= abs(pos/grid_spacing-round(pos/grid_spacing)) < Common.float_tolerance
446 if testpass:
return 'pass'
451 for dim
in range(2,4):
452 matrix = numpy.matrix(numpy.ones([dim,dim]))
453 for (i,j),value
in numpy.ndenumerate(matrix):
454 if i != j: matrix[i,j] *= 1./float(i+1)/float(j+1)
455 matrix_inverse = numpy.linalg.inv(matrix)
456 shell = Common.make_shell(n_per_dim, matrix)
458 if not (len(shell)==n_per_dim**dim
or len(shell)==n_per_dim**dim-1):
462 if abs( (vector*matrix_inverse*vector.transpose())[0,0]-1. )>Common.float_tolerance:
469 raise RuntimeError(
'Throwing item '+str(item))
470 time.sleep(random.random())
474 queue = multiprocessing.Queue()
475 Common.__function_with_queue((__test_run, queue, (
"input",), 5))
476 assert queue.get() == (5, (
"output",
"input"))
477 Common.__function_with_queue((__test_run, queue, (
"throw",), 7))
481 Common.__function_with_queue((
'no_args',))
487 process_out = Common.process_list(__test_run, [(i,)
for i
in range(10)], 3)
489 assert process_out[i] == (
'output',i)
493 tests = [common_substitute_test, common_min_max_test, common_multisort_test, common_histogram_test, common_get_bin_edges_test, common_subprocess_test, common_kill_subprocesses_test, common_n_bins_test, common_normalise_vector_test]
497 Common.has_multiprocessing()
498 tests = [common_test_function_with_queue, common_test_process_list]
501 test_results.append(
'Warning - could not find multiprocessing. Skipping multiprocessing dependent tests')
506 tests = [common_has_numpy_test, common_nd_newton_raphson1_test, common_nd_newton_raphson2_test]
509 test_results.append(
'Warning - could not find NumPy. Skipping NumPy dependent tests')
513 tests = [common_has_root_test, common_wait_for_root_test, common_make_root_canvas_test, common_make_root_graph_test, common_make_root_histogram_test,
514 common_make_root_multigraph_test, common_make_root_multigraph_test, common_kolmogorov_smirnov_test_test, common_make_grid_test, common_make_shell_test]
517 test_results.append(
'Warning - could not find ROOT. Skipping ROOT dependent tests')
521 tests = [common_has_matplot_test, common_wait_for_matplot_test, common_make_matplot_graph_test, common_make_matplot_histogram_test,
522 common_make_matplot_multigraph_test, common_make_matplot_scatter_test, common_show_matplot_and_continue_test]
525 test_results.append(
'Warning - could not find MatPlotLib. Skipping MatPlotLib dependent tests')
529 print '\n============\n|| COMMON ||\n============'
530 print 'Passed ',results[0],
' tests\nFailed ',results[1],
' tests\n',results[2],
' warnings\n\n\n'
533 if __name__ ==
"__main__":