ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change
on this file since aa4b8379 was
103a0b0,
checked in by Mathieu Doucet <doucetm@…>, 17 years ago
|
Initial import for Park integration (docs only)
|
-
Property mode set to
100644
|
File size:
1.9 KB
|
Line | |
---|
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 | class Generator: |
---|
9 | ## Parameter A |
---|
10 | constant_a = 2.5 |
---|
11 | ## Parameter B |
---|
12 | constant_b = 4.0 |
---|
13 | ## Randomness factor |
---|
14 | randomness = 0.15 |
---|
15 | |
---|
16 | def __init__(self): |
---|
17 | pass |
---|
18 | |
---|
19 | def create(self, filename, xmin=0.0, xmax=10.0, npts=50): |
---|
20 | """ |
---|
21 | Create files with the generate data |
---|
22 | The file names will be: |
---|
23 | - Set #1: [filename]_line.txt |
---|
24 | - Set #2: [filename]_cst.txt |
---|
25 | |
---|
26 | @param filename: string to prepend to the file name |
---|
27 | @param xmin: minimum x-value |
---|
28 | @param xmax: maximum x-value |
---|
29 | @param npts: number of points to generate |
---|
30 | """ |
---|
31 | import random, time |
---|
32 | random.seed(time.time()) |
---|
33 | |
---|
34 | # Write line data set |
---|
35 | fd = open(filename+'_line.txt', 'w') |
---|
36 | fd.write("#y=A*x+B\n#A=%g\n#B=%g\n" % (self.constant_a, self.constant_b)) |
---|
37 | |
---|
38 | for i in range(npts): |
---|
39 | x = xmin+(xmax-xmin)/(npts-1)*i |
---|
40 | mu = self.constant_a*x+self.constant_b |
---|
41 | err = self.randomness*mu |
---|
42 | y = random.gauss(mu, err) |
---|
43 | fd.write("%g %g %g\n" % (x,y,err)) |
---|
44 | |
---|
45 | fd.close() |
---|
46 | |
---|
47 | # Write constant data set |
---|
48 | fd = open(filename+'_cst.txt', 'w') |
---|
49 | fd.write("#y=A\n#A=%g\n" % self.constant_a) |
---|
50 | |
---|
51 | for i in range(npts): |
---|
52 | x = xmin+(xmax-xmin)/(npts-1)*i |
---|
53 | err = self.randomness*self.constant_a |
---|
54 | y = random.gauss(self.constant_a, err) |
---|
55 | fd.write("%g %g %g\n" % (x,y,err)) |
---|
56 | |
---|
57 | fd.close() |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | if __name__ == "__main__": |
---|
62 | gen = Generator() |
---|
63 | gen.create("testdata") |
---|
64 | print "Files created" |
---|
65 | |
---|
66 | |
---|
67 | |
---|
Note: See
TracBrowser
for help on using the repository browser.