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