1 | """ |
---|
2 | Generate two correlated sets of data |
---|
3 | 1- A line: y = ax + b |
---|
4 | 2- A constant equal to a of set #1 |
---|
5 | |
---|
6 | """ |
---|
7 | |
---|
8 | def get_x(x, y=0, dx=0, dy=0): |
---|
9 | return x |
---|
10 | |
---|
11 | def get_err_x(x, y=0, dx=0, dy=0): |
---|
12 | return dx |
---|
13 | |
---|
14 | |
---|
15 | class Generator: |
---|
16 | ## Parameter A |
---|
17 | constant_a = 2.5 |
---|
18 | ## Parameter B |
---|
19 | constant_b = 4.0 |
---|
20 | ## Randomness factor |
---|
21 | randomness = 0.07 |
---|
22 | |
---|
23 | def __init__(self): |
---|
24 | pass |
---|
25 | |
---|
26 | def create(self, filename, xmin=0.01, xmax=10.0, npts=50, |
---|
27 | xfunc=get_x, yfunc=get_x, errfunc=get_err_x): |
---|
28 | """ |
---|
29 | Create files with the generate data |
---|
30 | The file names will be: |
---|
31 | - Set #1: [filename]_line.txt |
---|
32 | - Set #2: [filename]_cst.txt |
---|
33 | |
---|
34 | @param filename: string to prepend to the file name |
---|
35 | @param xmin: minimum x-value |
---|
36 | @param xmax: maximum x-value |
---|
37 | @param npts: number of points to generate |
---|
38 | """ |
---|
39 | import random, time |
---|
40 | random.seed(time.time()) |
---|
41 | |
---|
42 | # Write line data set |
---|
43 | fd = open(filename, 'w') |
---|
44 | print "Creating ", filename |
---|
45 | fd.write("#y=A*x+B\n#A=%g\n#B=%g\n" % (self.constant_a, self.constant_b)) |
---|
46 | |
---|
47 | for i in range(npts): |
---|
48 | x = xmin+(xmax-xmin)/(npts-1)*i |
---|
49 | mu = self.constant_a*x+self.constant_b |
---|
50 | err = self.randomness*mu |
---|
51 | y = random.gauss(mu, err) |
---|
52 | fd.write("%g %g %g\n" % (xfunc(x, y), yfunc(y, xfunc(x, 0)), errfunc(y, xfunc(x,y), err, 0))) |
---|
53 | |
---|
54 | fd.close() |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | |
---|
59 | if __name__ == "__main__": |
---|
60 | from test_transfo import * |
---|
61 | gen = Generator() |
---|
62 | |
---|
63 | # Linear x series |
---|
64 | gen.create("linear.txt") |
---|
65 | gen.create("x_y2.txt", yfunc=from_x2, errfunc=err_x2) |
---|
66 | gen.create("x_inv_y.txt", yfunc=from_inv_x, errfunc=err_inv_x) |
---|
67 | gen.create("x_inv_sqrty.txt", yfunc=from_inv_sqrtx, errfunc=err_inv_sqrtx) |
---|
68 | gen.create("x_lny.txt", xmin=0.0001, xmax=3.0, yfunc=from_lnx, errfunc=err_lnx) |
---|
69 | gen.create("x_logy.txt", xmin=0.0001, xmax=3.0, yfunc=from_log10, errfunc=err_log10) |
---|
70 | gen.create("x_lnxy.txt", yfunc=from_lnxy, errfunc=err_lnxy) |
---|
71 | gen.create("x_lnyx2.txt", xmin=0.0001, xmax=10.0, yfunc=from_lnx2y, errfunc=err_lnx2y) |
---|
72 | gen.create("x_lnyx4.txt", xmin=0.0001, xmax=10.0, yfunc=from_lnx4y, errfunc=err_lnx4y) |
---|
73 | |
---|
74 | # Log10(x) |
---|
75 | gen.create("logx_y.txt", xmax=3.0, xfunc=from_log10) |
---|
76 | gen.create("logx_y2.txt", xmax=3.0, xfunc=from_log10, yfunc=from_x2, errfunc=err_x2) |
---|
77 | gen.create("logx_inv_y.txt", xmax=3.0, xfunc=from_log10, yfunc=from_inv_x, errfunc=err_inv_x) |
---|
78 | gen.create("logx_inv_sqrty.txt", xmax=3.0, xfunc=from_log10, yfunc=from_inv_sqrtx, errfunc=err_inv_sqrtx) |
---|
79 | gen.create("logx_lny.txt", xfunc=from_log10, xmin=0.0001, xmax=3.0, yfunc=from_lnx, errfunc=err_lnx) |
---|
80 | gen.create("logx_logy.txt", xfunc=from_log10, xmin=0.0001, xmax=3.0, yfunc=from_log10, errfunc=err_log10) |
---|
81 | gen.create("logx_lnxy.txt", xfunc=from_log10, yfunc=from_lnxy, errfunc=err_lnxy) |
---|
82 | gen.create("logx_lnyx2.txt", xfunc=from_log10, xmin=0.0001, xmax=10.0, yfunc=from_lnx2y, errfunc=err_lnx2y) |
---|
83 | gen.create("logx_lnyx4.txt", xfunc=from_log10, xmin=0.0001, xmax=10.0, yfunc=from_lnx4y, errfunc=err_lnx4y) |
---|
84 | |
---|
85 | # x^2 |
---|
86 | gen.create("x2_y.txt", xfunc=from_x2) |
---|
87 | gen.create("x2_y2.txt", xfunc=from_x2, yfunc=from_x2, errfunc=err_x2) |
---|
88 | gen.create("x2_inv_y.txt", xfunc=from_x2, yfunc=from_inv_x, errfunc=err_inv_x) |
---|
89 | gen.create("x2_inv_sqrty.txt", xfunc=from_x2, yfunc=from_inv_sqrtx, errfunc=err_inv_sqrtx) |
---|
90 | gen.create("x2_lny.txt", xfunc=from_x2, xmin=0.0001, xmax=3.0, yfunc=from_lnx, errfunc=err_lnx) |
---|
91 | gen.create("x2_logy.txt", xfunc=from_x2, xmin=0.0001, xmax=3.0, yfunc=from_log10, errfunc=err_log10) |
---|
92 | gen.create("x2_lnxy.txt", xfunc=from_x2, yfunc=from_lnxy, errfunc=err_lnxy) |
---|
93 | gen.create("x2_lnyx2.txt", xfunc=from_x2, xmin=0.0001, xmax=10.0, yfunc=from_lnx2y, errfunc=err_lnx2y) |
---|
94 | gen.create("x2_lnyx4.txt", xfunc=from_x2, xmin=0.0001, xmax=10.0, yfunc=from_lnx4y, errfunc=err_lnx4y) |
---|
95 | |
---|
96 | |
---|
97 | |
---|