Changeset 13420 for fact/tools/pyscripts
- Timestamp:
- 04/23/12 19:18:56 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/pyscripts/pyfact/generator.py
r13328 r13420 98 98 99 99 100 class SignalGeneratorCSV(object): 101 102 def __init__(self, file_name, option_str = 'len 100 noise 3', name = 'SignalGenerator'): 103 time, maxprob, mean, median = np.loadtxt( file_name, delimiter=',', unpack=True) 104 csv_data = maxprob 105 106 # csv data was downshifted, I shift it up here 107 self.csv_data = csv_data - csv_data.min() 108 109 110 self.__module__ = 'CSV generator' 111 self.option_str = option_str.lower() 112 self.options = make_options_from_str(option_str) 113 self.parse_options() 114 self.name = name 115 116 def parse_options(self): 117 o = self.options #shortcut 118 if 'len' in o: 119 self.npoints = int(o['len'][0]) 120 else: 121 self.npoints = 100 122 if 'noise' in o: 123 self.sigma = float(o['noise'][0]) 124 else: 125 self.sigma = 1 126 if 'bsl' in o: 127 self.bsl = float(o['bsl'][0]) 128 else: 129 self.bsl = -0.5 130 131 if 'step' in o: 132 self.step_height = float(o['step'][0]) 133 self.step_start = int(o['step'][1]) 134 self.step_stop = int(o['step'][2]) 135 136 if 'triangle' in o: 137 self.pulses = [] 138 # append 1st pulse to list of pulses 139 self.pulses.append( ( float(o['triangle'][0]) , float(o['triangle'][1]), int(o['triangle'][2]), int(o['triangle'][3]) ) ) 140 number_of_pulses_after_1st = (len(o['triangle'])-4)/2 141 for i in range(number_of_pulses_after_1st): 142 self.pulses.append( ( float(o['triangle'][2*i+4]) , float(o['triangle'][2*i+5]), int(o['triangle'][2]), int(o['triangle'][3]) ) ) 143 144 if 'spike' in o: 145 self.spikes = [] 146 for i in range(len(o['spike'])/2): 147 self.spikes.append( ( int(o['spike'][2*i]), float(o['spike'][2*i+1]) ) ) 148 149 if 'csv' in o: 150 self.csvs = [] 151 for i in range(len(o['csv'])/2): 152 time = int( o['csv'][2*i] ) 153 amplitude = float( o['csv'][2*i+1] ) 154 self.csvs.append( (time, amplitude) ) 155 156 def __call__(self, option_str = ''): 157 if option_str: 158 self.option_str = option_str.lower() 159 self.options = make_options_from_str(self.option_str) 160 self.parse_options() 161 162 signal = np.zeros(self.npoints) 163 signal += self.bsl 164 165 if 'step' in self.options: 166 signal[self.step_start:self.step_stop] += self.step_height 167 if 'triangle' in self.options: 168 for pulse in self.pulses: 169 pos = pulse[0] 170 height = pulse[1] 171 rise = pulse[2] 172 fall = pulse[3] 173 start = pos - rise 174 stop = pos + fall 175 signal[start:pos] += np.linspace(0., height, rise) 176 signal[pos:stop] += np.linspace(height, 0. , fall) 177 if 'spike' in self.options: 178 for spike in self.spikes: 179 signal[spike[0]] += spike[1] 180 if 'csv' in self.options: 181 for csv in self.csvs: 182 amplitude = csv[1] 183 time = csv[0] 184 csv_data = self.csv_data.copy() 185 #scale 186 csv_data *= amplitude 187 # add shifted 188 print 'bumm', len(csv_data) 189 print csv_data.shape 190 print signal[time:time+len(csv_data)].shape 191 signal[time:time+len(csv_data)] += csv_data 192 193 # add noise 194 signal += + np.random.normal(0.0,self.sigma, signal.shape) 195 return signal 196 197 def __str__(self): 198 s = self.name + '\n' 199 s += 'possible options and parameters\n' 200 s += ' * len: number of samples (100)\n' 201 s += ' * noise: sigma (1)\n' 202 s += ' * bsl: level (-0.5)\n' 203 s += ' * step: height, start, end\n' 204 s += ' * triangle: pos height risingedge, fallingedge [pos height ...]\n' 205 s += ' * spike: pos height [pos height ...]\n' 206 s += ' * csv: pos height [pos height ...]\n' 207 208 s += 'current options are:\n' 209 for key in self.options.keys(): 210 s += key + ':' + str(self.options[key]) + '\n' 211 return s 212 213 100 214 # Helper function to parse signalname and create a dictionary 101 215 # dictionary layout : … … 115 229 return options 116 230 117 def _plotter(signal, text):118 x=range(len(signal))119 ax = plt.plot(x, signal, 'b.', label='signal')120 plt.title('test of SignalGenerator with option string:\n' + text)121 plt.xlabel('sample')122 plt.legend()123 plt.grid(True)124 plt.show()125 126 231 if __name__ == '__main__': 127 import matplotlib.pyplot as plt 128 """ test the class """ 129 myGenerator = SignalGenerator('len 400 noise 0.3 bsl -2.5 triangle 50 10.2 10 100 65 10 150 20 180 10 250 10 spike 100 50. 20 50 21 49') 232 from plotters import Plotter 233 myGenerator = SignalGenerator('len 400 noise 0.3 bsl -2.5 triangle 50 10.2 10 100 65 10 150 20 spike 100 50. 20 50 21 49') 130 234 sig = myGenerator() 131 235 print myGenerator 132 _plotter(sig, myGenerator.option_str) 236 237 p = Plotter('generator test') 238 p(sig) 239 240 anothergen = SignalGeneratorCSV('PulseTemplate_PointSet_0.csv', 241 'len 1000 noise 0.4 bsl -2.0 csv 300 1 60 2 650 1 spike 110 50') 242 sig2 = anothergen() 243 print anothergen 244 245 pp = Plotter('CSV Gen Test') 246 pp(sig2) 247 248 raw_input('any key to quit')
Note:
See TracChangeset
for help on using the changeset viewer.