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