57d6b18d41e02f46802b358acbb615e4db01fbc8
[hooke.git] / hooke / ui / gui / driver.py
1
2     def _generate_vectors(self):
3         """
4         Here we parse the data and generate the raw vectors. This
5         method has only to do with the peculiar file format here, so
6         it's of no big interest (I just wrote it to present a
7         functional driver).
8
9         Only thing to remember, it can be nice to call methods that
10         are used only "internally" by the driver (or by plugins) with
11         a "_" prefix, so to have a visual remark. But it's just an
12         advice.
13         """
14         vectors={'PLOT1':[[],[],[],[]] , 'PLOT2':[[],[],[],[]]}
15         positions={'X1':0,'Y1':1,'X2':2,'Y2':3}
16         whatplot=''
17         pos=0
18         for item in self.data:
19             try:
20                 num=float(item)
21                 vectors[whatplot][pos].append(num)
22             except ValueError:
23                 if item[:-1]=='PLOT1':
24                     whatplot=item[:-1]
25                 elif item[:-1]=='PLOT2':
26                     whatplot=item[:-1]
27                 elif item[0]=='X' or item[0]=='Y':
28                     pos=positions[item[:-1]]
29                 else:
30                     pass
31
32         return vectors
33
34     def default_plots(self):
35         """
36         THIS METHOD MUST BE DEFINED.
37         RETURNS: [ lhc.PlotObject ] or [ lhc.PlotObject, lhc.PlotObject]
38
39         This is the method that returns the plots to Hooke.
40         It must return a list with *one* or *two* PlotObjects.
41
42         See the curve.py source code to see how PlotObjects are defined and work in detail.
43         """
44         gen_vectors=self._generate_vectors()
45
46         #Here we create the main plot PlotObject and initialize its vectors.
47         main_plot=lhc.PlotObject()
48         main_plot.vectors=[]
49         #The same for the other plot.
50         other_plot=lhc.PlotObject()
51         other_plot.vectors=[]
52
53         """
54         Now we fill the plot vectors with our data.
55                                                            set 1                           set 2
56         The "correct" shape of the vector is [ [[x1,x2,x3...],[y1,y2,y3...]] , [[x1,x2,x3...],[y1,y2,y3...]] ], so we have to put stuff in this way into it.
57
58         The add_set() method takes care of this , just use plot.add_set(x,y).
59         """
60         main_plot.add_set(gen_vectors['PLOT1'][0],gen_vectors['PLOT1'][1])
61         main_plot.add_set(gen_vectors['PLOT1'][2],gen_vectors['PLOT1'][3])
62
63         other_plot.add_set(gen_vectors['PLOT2'][0],gen_vectors['PLOT2'][1])
64         other_plot.add_set(gen_vectors['PLOT2'][2],gen_vectors['PLOT2'][3])
65
66         """
67         normalize_vectors() trims the vectors, so that if two x/y couples are of different lengths, the latest
68         points are trimmed (otherwise we have a python error). Always a good idea to run it, to avoid crashes.
69         """
70         main_plot.normalize_vectors()
71         other_plot.normalize_vectors()
72
73         """
74         Here we define:
75         - units: [string, string], define the measure units of X and Y axes
76         - destination: 0/1 , defines where to plot the plot (0=top, 1=bottom), default=0
77         - title: string , the plot title.
78
79         for each plot.
80         Again, see curve.py comments for details.
81         """
82         main_plot.units=['unit of x','unit of y']
83         main_plot.destination=0
84         main_plot.title=self.filename+' main'
85
86         other_plot.units=['unit of x','unit of y']
87         other_plot.destination=1
88         other_plot.title=self.filename+' other'
89
90         return [main_plot, other_plot]