From 7fd76e5d9916a51c4015a560de4935fdb9d12df0 Mon Sep 17 00:00:00 2001 From: Yngve Inntjore Levinsen <Yngve.Levinsen@esss.se> Date: Mon, 12 Mar 2018 17:03:39 +0100 Subject: [PATCH] flake8 corrections --- ess/TraceWin.py | 818 ++++++++++++++++++++++++------------------------ 1 file changed, 407 insertions(+), 411 deletions(-) diff --git a/ess/TraceWin.py b/ess/TraceWin.py index 45ddf39..d3bd2ca 100644 --- a/ess/TraceWin.py +++ b/ess/TraceWin.py @@ -1,8 +1,9 @@ from __future__ import print_function + class dst: ''' - Simple class to read in a + Simple class to read in a TraceWin distribution file Class afterwards hold the following @@ -16,22 +17,21 @@ class dst: ''' def __init__(self, filename=None): # easy storage.. - self.filename=filename + self.filename = filename # used to create dict behaviour.. - self._columns=['x','xp','y','yp','phi','E'] + self._columns = ['x', 'xp', 'y', 'yp', 'phi', 'E'] if filename: # read in the file.. self._readBinaryFile() else: import numpy - self.Np=0 - self.Ib=0.0 - self.freq=352.21 - self._data=numpy.zeros((self.Np,6)) - self.mass=0.0 - + self.Np = 0 + self.Ib = 0.0 + self.freq = 352.21 + self._data = numpy.zeros((self.Np, 6)) + self.mass = 0.0 - def append(self,x=0.0,xp=0.0,y=0.0,yp=0.0,E=0.0,phi=0.0): + def append(self, x=0.0, xp=0.0, y=0.0, yp=0.0, E=0.0, phi=0.0): ''' Append one particle to the distribution @@ -42,9 +42,8 @@ class dst: ''' import numpy - self._data=numpy.append(self._data, [[x,xp,y,yp,phi,E]], 0) - self.Np+=1 - + self._data = numpy.append(self._data, [[x, xp, y, yp, phi, E]], 0) + self.Np += 1 def remove(self, i=None): ''' @@ -52,11 +51,11 @@ class dst: ''' import numpy - if i==None: - self._data=numpy.delete(self._data,numpy.s_[:],0) + if i is None: + self._data=numpy.delete(self._data,numpy.s_[:], 0) self.Np=0 else: - self._data=numpy.delete(self._data,i,0) + self._data=numpy.delete(self._data, i, 0) self.Np-=1 def _readBinaryFile(self): @@ -74,25 +73,25 @@ class dst: ('freq', numpy.float64), ('dummy3', numpy.int8) ]) - Header=numpy.fromfile(fin, dtype=Header_type, count=1) - self.Np=Header['Np'][0] - self.Ib=Header['Ib'][0] - self.freq=Header['freq'][0] + Header = numpy.fromfile(fin, dtype=Header_type, count=1) + self.Np = Header['Np'][0] + self.Ib = Header['Ib'][0] + self.freq = Header['freq'][0] # Some toutatis distributions has an undocumented 7th line of 0's - Table=numpy.fromfile(fin, dtype=numpy.float64, count=self.Np*7+1) + Table = numpy.fromfile(fin, dtype=numpy.float64, count=self.Np*7+1) if len(Table)==self.Np*7+1: - self._data=Table[:-1].reshape(self.Np,7) + self._data = Table[:-1].reshape(self.Np, 7) elif len(Table)==self.Np*6+1: # this is true in most cases - self._data=Table[:-1].reshape(self.Np,6) + self._data = Table[:-1].reshape(self.Np, 6) else: - raise ValueError("Incorrect table dimensions found:",len(Table)) + raise ValueError("Incorrect table dimensions found:", len(Table)) # convert x,y from cm to m: - self._data[:,0]*=1e-2 - self._data[:,2]*=1e-2 + self._data[:,0] *= 1e-2 + self._data[:,2] *= 1e-2 - self.mass=Table[-1] + self.mass = Table[-1] def keys(self): return self._columns[:] @@ -102,14 +101,14 @@ class dst: # e.g. dst['x'] returns the x array.. try: i=self._columns.index(key) - return self._data[:,i] + return self._data[:, i] except: raise ValueError("Available keys: "+str(self._columns)) def __setitem__(self, key, value): try: i=self._columns.index(key) - self._data[:,i]=value + self._data[:,i] = value except: raise ValueError("Available keys: "+str(self._columns)) @@ -127,34 +126,34 @@ class dst: from struct import pack import numpy - fout=open(filename,'wb') - fout.write(pack('b',125)) - fout.write(pack('b',100)) - fout.write(pack('i',self.Np)) - fout.write(pack('d',self.Ib)) - fout.write(pack('d',self.freq)) - fout.write(pack('b',125)) + fout=open(filename, 'wb') + fout.write(pack('b', 125)) + fout.write(pack('b', 100)) + fout.write(pack('i', self.Np)) + fout.write(pack('d', self.Ib)) + fout.write(pack('d', self.freq)) + fout.write(pack('b', 125)) - data=self._data.copy() + data = self._data.copy() if toutatis and data.shape[1]==6: - data=numpy.append(data,numpy.zeros((len(data),1)),1) + data = numpy.append(data,numpy.zeros((len(data),1)),1) elif not toutatis and data.shape[1]==7: - data=data[:,:-1] + data = data[:,:-1] # convert x,y from m to cm: - data[:,0]*=1e2 - data[:,2]*=1e2 + data[:,0] *= 1e2 + data[:,2] *= 1e2 if toutatis: - data=data.reshape(self.Np*7,1) + data = data.reshape(self.Np*7, 1) else: - data=data.reshape(self.Np*6,1) + data = data.reshape(self.Np*6, 1) for x in data: - fout.write(pack('d',x)) + fout.write(pack('d', x)) - fout.write(pack('d',self.mass)) + fout.write(pack('d', self.mass)) fout.close() def subplot(self, index, x, y=None, nb=100, mask=None): @@ -185,9 +184,9 @@ class dst: # get X and Y data dx=np.array(self[x]) if mask!=None: - dx=dx[mask] + dx = dx[mask] if y!=None: - dy=np.array(self[y]) + dy = np.array(self[y]) if mask!=None: dy=dy[mask] @@ -384,16 +383,16 @@ class plt: if not hasattr(self,'avg'): self.calc_avg() - self.gamma=[] - self.beta=[] + self.gamma = [] + self.beta = [] for i,j in zip(self.Nelp,range(len(self.Nelp))): - Eavg=self.avg['E'][j] - self.gamma.append((self.mc2+Eavg)/self.mc2) - self.beta.append(numpy.sqrt(1.-1./self.gamma[-1]**2)) - self.gamma=numpy.array(self.gamma) - self.beta=numpy.array(self.beta) + Eavg = self.avg['E'][j] + self.gamma.append((self.mc2 + Eavg) / self.mc2) + self.beta.append(numpy.sqrt(1. - 1. / self.gamma[-1]**2)) + self.gamma = numpy.array(self.gamma) + self.beta = numpy.array(self.beta) - def calc_minmax(self,pmin=5,pmax=95): + def calc_minmax(self, pmin=5, pmax=95): ''' Calculates min/max values of beam coordinates in percentile, pmin is lower and pmax upper. @@ -402,14 +401,14 @@ class plt: ''' import numpy - self.min=dict(x=[], xp=[], y=[], yp=[], E=[]) - self.max=dict(x=[], xp=[], y=[], yp=[], E=[]) + self.min = dict(x=[], xp=[], y=[], yp=[], E=[]) + self.max = dict(x=[], xp=[], y=[], yp=[], E=[]) for i in self.Nelp: - data=self[i] + data = self[i] for v in self.min.keys(): - self.min[v].append(numpy.percentile(data[v],pmin)) - self.max[v].append(numpy.percentile(data[v],pmax)) + self.min[v].append(numpy.percentile(data[v], pmin)) + self.max[v].append(numpy.percentile(data[v], pmax)) for v in self.min.keys(): self.min[v]=numpy.array(self.min[v]) @@ -434,18 +433,18 @@ class plt: import numpy if not hasattr(self,'avg'): - self.calc_avg() + self.calc_avg() - vals=self._columns[:-1] + vals = self._columns[:-1] - self.sigma=[] + self.sigma = [] for j in range(len(self.Nelp)): - i=self.Nelp[j] - data=self[i] + i = self.Nelp[j] + data = self[i] - self.sigma.append([[numpy.mean( (data[n]-self.avg[n][j]) * (data[m] - self.avg[m][j]) ) for n in vals] for m in vals]) + self.sigma.append([[numpy.mean((data[n] - self.avg[n][j]) * (data[m] - self.avg[m][j])) for n in vals] for m in vals]) - self.sigma=numpy.array(self.sigma) + self.sigma = numpy.array(self.sigma) def calc_std(self): ''' @@ -455,16 +454,16 @@ class plt: import numpy - if not hasattr(self,'sigma'): - self.calc_sigma() + if not hasattr(self, 'sigma'): + self.calc_sigma() - vals=self._columns[:-1] + vals = self._columns[:-1] - self.std={} + self.std = {} for j in range(len(vals)): - v=vals[j] - self.std[v]=numpy.sqrt(self.sigma[:,j,j]) + v = vals[j] + self.std[v] = numpy.sqrt(self.sigma[:, j, j]) def calc_twiss(self): ''' @@ -474,29 +473,29 @@ class plt: import numpy - if not hasattr(self,'sigma'): - self.calc_sigma() - if not hasattr(self,'gamma'): - self.calc_rel() + if not hasattr(self, 'sigma'): + self.calc_sigma() + if not hasattr(self, 'gamma'): + self.calc_rel() - self.twiss_eps=[] + self.twiss_eps = [] for j in range(len(self.Nelp)): - self.twiss_eps.append([numpy.sqrt(numpy.linalg.det(self.sigma[j][i:i+2][:,i:i+2])) for i in (0,2,4)]) - self.twiss_eps=numpy.array(self.twiss_eps) + self.twiss_eps.append([numpy.sqrt(numpy.linalg.det(self.sigma[j][i:i + 2][:, i:i + 2])) for i in (0, 2, 4)]) + self.twiss_eps = numpy.array(self.twiss_eps) # Calculate normalized emittance: # TODO: this is NOT correct normalization for longitudinal - self.twiss_eps_normed=self.twiss_eps.copy() + self.twiss_eps_normed = self.twiss_eps.copy() for i in range(3): - self.twiss_eps_normed[:,i]*=self.gamma*self.beta + self.twiss_eps_normed[:, i] *= self.gamma * self.beta # Calculate beta: # This is a factor 10 different from what TraceWin plots - self.twiss_beta = [[self.sigma[j][i][i]/self.twiss_eps[j,i/2] for i in (0,2,4)] for j in range(len(self.Nelp))] + self.twiss_beta = [[self.sigma[j][i][i] / self.twiss_eps[j, i / 2] for i in (0, 2, 4)] for j in range(len(self.Nelp))] self.twiss_beta = numpy.array(self.twiss_beta) # Calculate alpha: - self.twiss_alpha = [[-self.sigma[j][i][i+1]/self.twiss_eps[j,i/2] for i in (0,2,4)] for j in range(len(self.Nelp))] + self.twiss_alpha = [[-self.sigma[j][i][i + 1] / self.twiss_eps[j, i / 2] for i in (0, 2, 4)] for j in range(len(self.Nelp))] self.twiss_alpha = numpy.array(self.twiss_alpha) def get_dst(self, index): @@ -504,34 +503,33 @@ class plt: Returns the dst corresponding to the given index ''' import numpy - - dset=self[index] - - _dst=dst() - _dst.freq=self.freq - _dst.Ib=self.Ib*1000 - _dst.Np=len(dset['x']) - _dst.mass=self.mc2 - _dst._data=numpy.array([dset['x'], - dset['xp'], - dset['y'], - dset['yp'], - dset['phi'], - dset['E']]).transpose() + + dset = self[index] + + _dst = dst() + _dst.freq = self.freq + _dst.Ib = self.Ib * 1000 + _dst.Np = len(dset['x']) + _dst.mass = self.mc2 + _dst._data = numpy.array([dset['x'], + dset['xp'], + dset['y'], + dset['yp'], + dset['phi'], + dset['E']]).transpose() return _dst - def save_dst(self, index,filename): + def save_dst(self, index, filename): ''' Saves the dst at the specified index to file Returns the same dst object. ''' - _dst=self.get_dst(index) + _dst = self.get_dst(index) _dst.save(filename) return _dst - class density_file: ''' Simple class to read a TraceWin density file @@ -539,152 +537,148 @@ class density_file: ''' def __init__(self, filename, envelope=None): - import numpy, sys - + import numpy + import sys - self.filename=filename - self.fin=open(self.filename, 'r') + self.filename = filename + self.fin = open(self.filename, 'r') - if envelope==None: # try to guess - if filename.split('/')[-1].split('.')[0]=='Density_Env': - self.envelope=True + if envelope is None: # try to guess + if filename.split('/')[-1].split('.')[0] == 'Density_Env': + self.envelope = True else: - self.envelope=False + self.envelope = False else: - self.envelope=envelope + self.envelope = envelope # currently unknown: - self.version=0 + self.version = 0 # first we simply count how many elements we have: - counter=0 + counter = 0 while True: try: self._skipAndCount() - counter+=1 - except IndexError: # EOF reached.. + counter += 1 + except IndexError: # EOF reached.. break if sys.flags.debug: print("Number of steps found:", counter) self.fin.seek(0) # set up the arrays.. - self.i=0 + self.i = 0 # z position [m] : - self.z=numpy.zeros(counter) + self.z = numpy.zeros(counter) # element index number - self.nelp=numpy.zeros(counter) + self.nelp = numpy.zeros(counter) # current [mA] : - self.ib=numpy.zeros(counter) + self.ib = numpy.zeros(counter) # number of lost particles: - self.Np=numpy.zeros(counter) + self.Np = numpy.zeros(counter) - self.Xouv=numpy.zeros(counter) - self.Youv=numpy.zeros(counter) + self.Xouv = numpy.zeros(counter) + self.Youv = numpy.zeros(counter) - if self.version>=9: - self.dXouv=numpy.zeros(counter) - self.dYouv=numpy.zeros(counter) + if self.version >= 9: + self.dXouv = numpy.zeros(counter) + self.dYouv = numpy.zeros(counter) - self.moy=numpy.zeros((counter,7)) - self.moy2=numpy.zeros((counter,7)) + self.moy = numpy.zeros((counter, 7)) + self.moy2 = numpy.zeros((counter, 7)) - self._max=numpy.zeros((counter,7)) - self._min=numpy.zeros((counter,7)) + self._max = numpy.zeros((counter, 7)) + self._min = numpy.zeros((counter, 7)) - if self.version>=10: - self.maxR=numpy.zeros((counter,7)) - self.minR=numpy.zeros((counter,7)) + if self.version >= 10: + self.maxR = numpy.zeros((counter, 7)) + self.minR = numpy.zeros((counter, 7)) - if self.version>=5: - self.rms_size=numpy.zeros((counter,7)) - self.rms_size2=numpy.zeros((counter,7)) + if self.version >= 5: + self.rms_size = numpy.zeros((counter, 7)) + self.rms_size2 = numpy.zeros((counter, 7)) - if self.version>=6: - self.min_pos_moy=numpy.zeros((counter,7)) - self.max_pos_moy=numpy.zeros((counter,7)) + if self.version >= 6: + self.min_pos_moy = numpy.zeros((counter, 7)) + self.max_pos_moy = numpy.zeros((counter, 7)) - if self.version>=7: - self.rms_emit=numpy.zeros((counter,3)) - self.rms_emit2=numpy.zeros((counter,3)) + if self.version >= 7: + self.rms_emit = numpy.zeros((counter, 3)) + self.rms_emit2 = numpy.zeros((counter, 3)) - if self.version>=8: - self.energy_accept=numpy.zeros(counter) - self.phase_ouv_pos=numpy.zeros(counter) - self.phase_ouv_neg=numpy.zeros(counter) + if self.version >= 8: + self.energy_accept = numpy.zeros(counter) + self.phase_ouv_pos = numpy.zeros(counter) + self.phase_ouv_neg = numpy.zeros(counter) - self.lost=numpy.zeros((counter,self.Nrun)) - self.powlost=numpy.zeros((counter,self.Nrun)) + self.lost = numpy.zeros((counter, self.Nrun)) + self.powlost = numpy.zeros((counter, self.Nrun)) - self.lost2=numpy.zeros(counter) - self.Minlost=numpy.zeros(counter) - self.Maxlost=numpy.zeros(counter) + self.lost2 = numpy.zeros(counter) + self.Minlost = numpy.zeros(counter) + self.Maxlost = numpy.zeros(counter) - self.powlost2=numpy.zeros(counter) - self.Minpowlost=numpy.zeros(counter) - self.Maxpowlost=numpy.zeros(counter) + self.powlost2 = numpy.zeros(counter) + self.Minpowlost = numpy.zeros(counter) + self.Maxpowlost = numpy.zeros(counter) - - while self.i<counter: + while self.i < counter: self._getFullContent() - self.i+=1 - if sys.flags.debug and self.i%100 == 0: - print('Read status',self.i) - + self.i += 1 + if sys.flags.debug and self.i % 100 == 0: + print('Read status', self.i) def _getHeader(self): import numpy - # header.. - version=numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0] - year=numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0] + version = numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0] + year = numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0] # in case we did not read all data, this will detect our mistake: - shift=0 - while year!=2011 or version not in [8,9,10,11,12]: - shift+=1 - version=year - year=numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0] + shift = 0 + while year != 2011 or version not in [8, 9, 10, 11, 12]: + shift += 1 + version = year + year = numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0] if shift: - print(year,version) - raise ValueError("ERROR, shifted "+str(shift*2)+" bytes") + print(year, version) + raise ValueError("ERROR, shifted " + str(shift * 2) + " bytes") - self.vlong=numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0] - self.Nrun=numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0] + self.vlong = numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0] + self.Nrun = numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0] - self.version=version - self.year=year + self.version = version + self.year = year def _skipAndCount(self): - import numpy self._getHeader() if self.envelope: - if self.version==8: - numpy.fromfile(self.fin, dtype=numpy.int16, count=292//2) - elif self.version==9: - numpy.fromfile(self.fin, dtype=numpy.int16, count=300//2) - elif self.version==10: - numpy.fromfile(self.fin, dtype=numpy.int16, count=356//2) + if self.version == 8: + numpy.fromfile(self.fin, dtype=numpy.int16, count=292 // 2) + elif self.version == 9: + numpy.fromfile(self.fin, dtype=numpy.int16, count=300 // 2) + elif self.version == 10: + numpy.fromfile(self.fin, dtype=numpy.int16, count=356 // 2) else: raise TypeError("It is not possible to read this format..") - elif self.Nrun>1: - #WARN not 100% sure if this is correct.. - if self.version<=9: - numpy.fromfile(self.fin, dtype=numpy.int16, count=((5588+self.Nrun*12)//2)) - elif self.version==10: - numpy.fromfile(self.fin, dtype=numpy.int16, count=((20796+self.Nrun*12)//2)) + elif self.Nrun > 1: + # WARN not 100% sure if this is correct.. + if self.version <= 9: + numpy.fromfile(self.fin, dtype=numpy.int16, count=((5588 + self.Nrun * 12) // 2)) + elif self.version == 10: + numpy.fromfile(self.fin, dtype=numpy.int16, count=((20796 + self.Nrun * 12) // 2)) else: raise TypeError("It is not possible to read this format..") - elif self.version==8: - numpy.fromfile(self.fin, dtype=numpy.int16, count=12344//2) - elif self.version==9: - numpy.fromfile(self.fin, dtype=numpy.int16, count=12352//2) - elif self.version==10: - numpy.fromfile(self.fin, dtype=numpy.int16, count=12408//2) + elif self.version == 8: + numpy.fromfile(self.fin, dtype=numpy.int16, count=12344 // 2) + elif self.version == 9: + numpy.fromfile(self.fin, dtype=numpy.int16, count=12352 // 2) + elif self.version == 10: + numpy.fromfile(self.fin, dtype=numpy.int16, count=12408 // 2) else: raise TypeError("It is not possible to read this format..") @@ -705,107 +699,107 @@ class density_file: import numpy - #self._getHeader() + # self._getHeader() # no need to read the header again: # (though only if we are SURE about content!) numpy.fromfile(self.fin, dtype=numpy.int16, count=5) - self.nelp[self.i]=numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0] - self.ib[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] - self.z[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] + self.nelp[self.i] = numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0] + self.ib[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] + self.z[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] # Aperture - self.Xouv[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] - self.Youv[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] - if self.version>=9: - dXouv=numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] - dYouv=numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] - step=numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0] - - n=7 # x [m], y[m], Phase [deg], Energy [MeV], R[m], Z[m], dp/p - - self.moy[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] - self.moy2[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] - - self._max[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] - self._min[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] - - if self.version>=10: - self.maxR[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] - self.minR[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] - - if self.version>=5: - self.rms_size[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n) - self.rms_size2[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n) - if self.version>=6: - self.min_pos_moy[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n) - self.max_pos_moy[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=n) - if self.version>=7: - self.rms_emit[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=3)[:] - self.rms_emit2[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=3)[:] - if self.version>=8: - self.energy_accept[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1) - self.phase_ouv_pos[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1) - self.phase_ouv_neg[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1) - - self.Np[self.i]=numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] + self.Xouv[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] + self.Youv[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] + if self.version >= 9: + dXouv = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] + dYouv = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] + step = numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0] + + n = 7 # x [m], y[m], Phase [deg], Energy [MeV], R[m], Z[m], dp/p + + self.moy[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] + self.moy2[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] + + self._max[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] + self._min[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] + + if self.version >= 10: + self.maxR[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] + self.minR[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:] + + if self.version >= 5: + self.rms_size[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n) + self.rms_size2[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n) + if self.version >= 6: + self.min_pos_moy[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n) + self.max_pos_moy[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n) + if self.version >= 7: + self.rms_emit[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=3)[:] + self.rms_emit2[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=3)[:] + if self.version >= 8: + self.energy_accept[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1) + self.phase_ouv_pos[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1) + self.phase_ouv_neg[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1) + + self.Np[self.i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] if self.Np[self.i]: for i in range(self.Nrun): - self.lost[self.i,i]=numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] - self.powlost[self.i,i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] - self.lost2[self.i]=numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] - self.Minlost[self.i]=numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] - self.Maxlost[self.i]=numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] - self.powlost2[self.i]=numpy.fromfile(self.fin, dtype=numpy.float64, count=1)[0] - self.Minpowlost[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] - self.Maxpowlost[self.i]=numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] - - if self.vlong==1: - tab=numpy.fromfile(self.fin, dtype=numpy.uint64, count=n*step) + self.lost[self.i, i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] + self.powlost[self.i, i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] + self.lost2[self.i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] + self.Minlost[self.i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] + self.Maxlost[self.i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0] + self.powlost2[self.i] = numpy.fromfile(self.fin, dtype=numpy.float64, count=1)[0] + self.Minpowlost[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] + self.Maxpowlost[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0] + + if self.vlong == 1: + tab = numpy.fromfile(self.fin, dtype=numpy.uint64, count=n * step) else: - tab=numpy.fromfile(self.fin, dtype=numpy.uint32, count=n*step) - - if self.ib[self.i]>0: - tabp=numpy.fromfile(self.fin, dtype=numpy.uint32, count=3*step) + tab = numpy.fromfile(self.fin, dtype=numpy.uint32, count=n * step) + + if self.ib[self.i] > 0: + tabp = numpy.fromfile(self.fin, dtype=numpy.uint32, count=3 * step) - def _avg_merge(self,other,param): + def _avg_merge(self, other, param): ''' returns the average of the parameter weighted by how many Nruns in self and other object This allows for different lengths of the two arrays.. ''' - mine=getattr(self,param) - new=getattr(other,param) - if len(mine)>len(new): + mine = getattr(self, param) + new = getattr(other, param) + if len(mine) > len(new): ret = mine.copy() - ret[:len(new)] = (mine[:len(new)]*self.Nrun+new*other.Nrun)/(self.Nrun+other.Nrun) - elif len(mine)<len(new): + ret[:len(new)] = (mine[:len(new)] * self.Nrun + new * other.Nrun) / (self.Nrun + other.Nrun) + elif len(mine) < len(new): ret = new.copy() - ret[:len(mine)] = (mine*self.Nrun+new[:len(mine)]*other.Nrun)/(self.Nrun+other.Nrun) + ret[:len(mine)] = (mine * self.Nrun + new[:len(mine)] * other.Nrun) / (self.Nrun + other.Nrun) else: - ret = (mine*self.Nrun+new*other.Nrun)/(self.Nrun+other.Nrun) + ret = (mine * self.Nrun + new * other.Nrun) / (self.Nrun + other.Nrun) return ret - def _sum_merge(self,other,param): + def _sum_merge(self, other, param): ''' returns the sum of the parameter This allows for different lengths of the two arrays.. ''' - mine=getattr(self,param) - new=getattr(other,param) - if len(mine)>len(new): + mine = getattr(self, param) + new = getattr(other, param) + if len(mine) > len(new): ret = mine.copy() ret[:len(new)] += new - elif len(mine)<len(new): + elif len(mine) < len(new): ret = new.copy() ret[:len(mine)] += mine else: - ret = mine+new + ret = mine + new return ret - def _concatenate_merge(self,other,param): + def _concatenate_merge(self, other, param): ''' returns the concatenation of the two matrices @@ -813,38 +807,38 @@ class density_file: ''' import numpy - mine=getattr(self,param) - new=getattr(other,param) - ret=numpy.zeros(( max([len(mine),len(new)]), len(mine[0])+len(new[0]) )) - ret[:len(mine),:len(mine[0])]=mine - ret[:len(new),len(mine[0]):]=new + mine = getattr(self, param) + new = getattr(other, param) + ret = numpy.zeros((max([len(mine), len(new)]), len(mine[0]) + len(new[0]))) + ret[:len(mine), :len(mine[0])] = mine + ret[:len(new), len(mine[0]):] = new return ret - def _fun_merge(self,other,function,param): + def _fun_merge(self, other, function, param): ''' returns the function applied on the parameter This allows for different lengths of the two arrays.. ''' - mine=getattr(self,param) - new=getattr(other,param) - if len(mine)>len(new): + mine = getattr(self, param) + new = getattr(other, param) + if len(mine) > len(new): ret = mine.copy() - ret[:len(new)] = function(mine[:len(new)],new) - elif len(mine)<len(new): + ret[:len(new)] = function(mine[:len(new)], new) + elif len(mine) < len(new): ret = new.copy() - ret[:len(mine)] = function(mine,new[:len(mine)]) + ret[:len(mine)] = function(mine, new[:len(mine)]) else: - ret = function(mine,new) + ret = function(mine, new) return ret - def merge(self,objects): + def merge(self, objects): ''' Merge with list of objects ''' import numpy - if not isinstance(objects,list): + if not isinstance(objects, list): raise TypeError("You tried to merge a non-list") # for now we only allow objects with same version.. @@ -854,90 +848,91 @@ class density_file: # merge info.. for o in objects: - if len(self.ib)<len(o.ib): + if len(self.ib) < len(o.ib): raise ValueError("Sorry, not implemented yet. Complain to Yngve") - self.ib=self._avg_merge(o,'ib') + self.ib = self._avg_merge(o, 'ib') # this looks strange to me, but it is what TraceWin does.. - self.moy=self._sum_merge(o,'moy') - self.moy2=self._sum_merge(o,'moy') + self.moy = self._sum_merge(o, 'moy') + self.moy2 = self._sum_merge(o, 'moy') - self._max=self._fun_merge(o,numpy.maximum,'_max') - self._min=self._fun_merge(o,numpy.minimum,'_min') + self._max = self._fun_merge(o, numpy.maximum, '_max') + self._min = self._fun_merge(o, numpy.minimum, '_min') - if self.version>=5: + if self.version >= 5: # this looks strange to me, but it is what TraceWin does.. - self.rms_size=self._sum_merge(o,'rms_size') - self.rms_size2=self._sum_merge(o,'rms_size2') + self.rms_size = self._sum_merge(o, 'rms_size') + self.rms_size2 = self._sum_merge(o, 'rms_size2') - if self.version>=6: - self.max_pos_moy=self._fun_merge(o,numpy.maximum,'max_pos_moy') - self.min_pos_moy=self._fun_merge(o,numpy.minimum,'min_pos_moy') + if self.version >= 6: + self.max_pos_moy = self._fun_merge(o, numpy.maximum, 'max_pos_moy') + self.min_pos_moy = self._fun_merge(o, numpy.minimum, 'min_pos_moy') - if self.version>=7: + if self.version >= 7: # this looks strange to me, but it is what TraceWin does.. - self.rms_emit=self._sum_merge(o,'rms_emit') - self.rms_emit2=self._sum_merge(o,'rms_emit2') + self.rms_emit = self._sum_merge(o, 'rms_emit') + self.rms_emit2 = self._sum_merge(o, 'rms_emit2') - if self.version>=8: + if self.version >= 8: # Warning: TraceWin does NOT merge these data in any way - self.energy_accept=self._avg_merge(o,'energy_accept') - self.phase_ouv_pos=self._avg_merge(o,'phase_ouv_pos') - self.phase_ouv_neg=self._avg_merge(o,'phase_ouv_neg') + self.energy_accept = self._avg_merge(o, 'energy_accept') + self.phase_ouv_pos = self._avg_merge(o, 'phase_ouv_pos') + self.phase_ouv_neg = self._avg_merge(o, 'phase_ouv_neg') # Note, we don't get into the problem of differing table sizes # particles are lost, because we have written zeroes for # the rest of the tables - self.lost=self._concatenate_merge(o,'lost') - self.powlost=self._concatenate_merge(o,'powlost') + self.lost = self._concatenate_merge(o, 'lost') + self.powlost = self._concatenate_merge(o, 'powlost') - self.lost2=self._sum_merge(o,'lost2') - self.powlost2=self._sum_merge(o,'powlost2') + self.lost2 = self._sum_merge(o, 'lost2') + self.powlost2 = self._sum_merge(o, 'powlost2') - self.Minlost=self._fun_merge(o,numpy.minimum,'Minlost') - self.Maxlost=self._fun_merge(o,numpy.maximum,'Maxlost') - self.Minpowlost=self._fun_merge(o,numpy.minimum,'Minpowlost') - self.Maxpowlost=self._fun_merge(o,numpy.maximum,'Maxpowlost') + self.Minlost = self._fun_merge(o, numpy.minimum, 'Minlost') + self.Maxlost = self._fun_merge(o, numpy.maximum, 'Maxlost') + self.Minpowlost = self._fun_merge(o, numpy.minimum, 'Minpowlost') + self.Maxpowlost = self._fun_merge(o, numpy.maximum, 'Maxpowlost') # Note: We are ignoring tab/tabp data... # merge final info (make sure to do this last!) - self.Np=self._sum_merge(o,'Np') - self.Nrun+=o.Nrun + self.Np = self._sum_merge(o, 'Np') + self.Nrun += o.Nrun - def savetohdf(self,filename='Density.h5',group='TraceWin', force=False): + def savetohdf(self, filename='Density.h5', group='TraceWin', force=False): ''' Saves data to HDF5 ''' - import h5py,sys + import h5py + import sys - fout = h5py.File(filename,'a') + fout = h5py.File(filename, 'a') if group in fout: if force: del fout[group] else: if sys.flags.debug: - print("Group {} already exist in {}".format(group,filename)) + print("Group {} already exist in {}".format(group, filename)) return group = fout.create_group(group) # header attributes.. - group.attrs['version']=self.version - group.attrs['year']=self.year - group.attrs['Nrun']=self.Nrun - group.attrs['vlong']=self.vlong + group.attrs['version'] = self.version + group.attrs['year'] = self.year + group.attrs['Nrun'] = self.Nrun + group.attrs['vlong'] = self.vlong - length=len(self.z) + length = len(self.z) - partran = sum(self.Np)>0 + partran = sum(self.Np) > 0 # one number per location - arrays= ['z', 'nelp', 'ib', 'Np', 'Xouv', 'Youv'] - array_units=['m', '', 'mA', '', 'm', 'm'] - if self.version>=8: + arrays = ['z', 'nelp', 'ib', 'Np', 'Xouv', 'Youv'] + array_units = ['m', '', 'mA', '', 'm', 'm'] + if self.version >= 8: arrays += ['energy_accept', 'phase_ouv_pos', 'phase_ouv_neg'] array_units += [ 'eV', 'deg', 'deg'] if partran: @@ -945,70 +940,69 @@ class density_file: array_units += [ '', '', '', 'W*w', 'W', 'W'] # 7 numbers per location.. - coordinates= ['moy', 'moy2', '_max', '_min'] - coordinate_units=['m', 'm*m', 'm', 'm'] - if self.version>=5 and partran: - coordinates += ['rms_size','rms_size2'] - coordinate_units += [ 'm', 'm*m'] - if self.version>=6 and partran: + coordinates = ['moy', 'moy2', '_max', '_min'] + coordinate_units = ['m', 'm*m', 'm', 'm'] + if self.version >= 5 and partran: + coordinates += ['rms_size', 'rms_size2'] + coordinate_units += [ 'm', 'm*m'] + if self.version >= 6 and partran: coordinates += ['min_pos_moy', 'max_pos_moy'] coordinate_units += [ 'm', 'm'] - - for val,unit in zip(arrays,array_units): - data_set=group.create_dataset(val, (length,), dtype='f') - data_set[...]=getattr(self,val) + for val, unit in zip(arrays, array_units): + data_set = group.create_dataset(val, (length,), dtype='f') + data_set[...] = getattr(self, val) if unit: - data_set.attrs['unit']=unit + data_set.attrs['unit'] = unit - for val,unit in zip(coordinates,coordinate_units): - data_set=group.create_dataset(val, (length,7), dtype='f') - data_set[...]=getattr(self,val) + for val, unit in zip(coordinates, coordinate_units): + data_set = group.create_dataset(val, (length, 7), dtype='f') + data_set[...] = getattr(self, val) if unit: - data_set.attrs['unit']=unit + data_set.attrs['unit'] = unit - if self.version>=7 and partran: + if self.version >= 7 and partran: # 3 numbers per location.. - emit_data=['rms_emit', 'rms_emit2'] - emit_units=['m*rad', 'm*m*rad*rad'] - for val,unit in zip(emit_data,emit_units): - data_set=group.create_dataset(val, (length,3), dtype='f') - data_set[...]=getattr(self,val) + emit_data = ['rms_emit', 'rms_emit2'] + emit_units = ['m*rad', 'm*m*rad*rad'] + for val, unit in zip(emit_data, emit_units): + data_set = group.create_dataset(val, (length, 3), dtype='f') + data_set[...] = getattr(self, val) if unit: - data_set.attrs['unit']=unit + data_set.attrs['unit'] = unit if partran: # 1 numbers per location and per run.. - data=['lost', 'powlost'] - units=['', 'W'] - for val,unit in zip(data, units): - data_set=group.create_dataset(val, (length,self.Nrun), dtype='f') - data_set[...]=getattr(self,val) + data = ['lost', 'powlost'] + units = ['', 'W'] + for val, unit in zip(data, units): + data_set = group.create_dataset(val, (length, self.Nrun), dtype='f') + data_set[...] = getattr(self, val) if unit: - data_set.attrs['unit']=unit + data_set.attrs['unit'] = unit fout.close() class remote_data_merger: def __init__(self, base='.'): - self._base=base - self._files=[] + self._base = base + self._files = [] - def add_file(self,filepath): + def add_file(self, filepath): import os if os.path.exists(filepath): - fname=filepath + fname = filepath else: - fullpath=os.path.join(self._base,filepath) + fullpath = os.path.join(self._base, filepath) if os.path.exists(fullpath): - fname=fullpath + fname = fullpath else: - raise ValueError("Could not find file "+filepath) + raise ValueError("Could not find file " + filepath) if fname not in self._files: self._files.append(fname) - def generate_partran_out(self,filename=None): + def generate_partran_out(self, filename=None): ''' Creates a string to be written to file each line is a list. @@ -1019,25 +1013,25 @@ class remote_data_merger: import numpy as np - h1=[] - h2=[] + h1 = [] + h2 = [] - d1=[] - d2=[] - d3=[] + d1 = [] + d2 = [] + d3 = [] if self._files: for f in self._files: - string=open(f,'r').read() - split=string.split('$$$') - if split[9]!='Data_Error': + string = open(f, 'r').read() + split = string.split('$$$') + if split[9] != 'Data_Error': raise ValueError("Magic problem, please complain to Yngve") - thisdata=split[10].strip().split('\n') + thisdata = split[10].strip().split('\n') if not h1: - h1=[thisdata[0]+" (std in paranthesis)"] - h2=thisdata[2:10] + h1 = [thisdata[0] + " (std in paranthesis)"] + h2 = thisdata[2:10] d1.append(thisdata[1].split()) d2.append(thisdata[10]) d3.append(thisdata[11]) @@ -1045,54 +1039,56 @@ class remote_data_merger: # fix d1: for i in range(len(d1)): for j in range(len(d1[0])): - d1[i][j]=float(d1[i][j]) - d1=np.array(d1) - means=d1.mean(axis=0) - stds=d1.std(axis=0) - d1=[] + d1[i][j] = float(d1[i][j]) + d1 = np.array(d1) + means = d1.mean(axis=0) + stds = d1.std(axis=0) + d1 = [] for i in range(len(stds)): - if stds[i]/means[i]<1e-10: - stds[i]=0.0 + if stds[i] / means[i] < 1e-10: + stds[i] = 0.0 for i in range(len(stds)): # some small std are removed.. - if stds[i]/means[i]>1e-8: - d1.append('%f(%f)' % (means[i],stds[i])) - else: #error is 0 + if stds[i] / means[i] > 1e-8: + d1.append('%f(%f)' % (means[i], stds[i])) + else: # error is 0 d1.append(str(means[i])) - d1=[' '.join(d1)] + d1 = [' '.join(d1)] # create data: - data=h1+d1+h2+d2+d3 + data = h1 + d1 + h2 + d2 + d3 if filename: - open(filename,'w').write('\n'.join(data)) + open(filename, 'w').write('\n'.join(data)) return data + class partran(dict): ''' Read partran1.out files.. ''' - def __init__(self,filename): - self.filename=filename + def __init__(self, filename): + self.filename = filename self._readAsciiFile() def _readAsciiFile(self): import numpy - stream=open(self.filename,'r') + stream = open(self.filename, 'r') for i in range(10): - line=stream.readline() - if line.strip()[0]=='#': + line = stream.readline() + if line.strip()[0] == '#': break - self.columns=['NUM']+line.split()[1:] - self.data=numpy.loadtxt(stream) + self.columns = ['NUM'] + line.split()[1:] + self.data = numpy.loadtxt(stream) - self._dict={} + self._dict = {} for i in range(len(self.columns)): - self[self.columns[i]]=self.data[:,i] + self[self.columns[i]] = self.data[:, i] + class field_map: ''' @@ -1102,47 +1098,47 @@ class field_map: ''' def __init__(self, filename): - self._filename=filename + self._filename = filename self._load_data(filename) - def _load_data(self,filename): - import os,numpy + def _load_data(self, filename): + import os + import numpy if not os.path.isfile(filename): raise ValueError("Cannot find file {}".format(filename)) - fin = open(filename,'r') - l=fin.readline().split() - self.start=[] - self.end=[] - numindexes=[] - while len(l)>1: - numindexes.append(int(l[0])+1) - if len(l)==2: + fin = open(filename, 'r') + l = fin.readline().split() + self.start = [] + self.end = [] + numindexes = [] + while len(l) > 1: + numindexes.append(int(l[0]) + 1) + if len(l) == 2: self.start.append(0.0) self.end.append(float(l[1])) else: self.start.append(float(l[1])) self.end.append(float(l[2])) - l=fin.readline().split() + l = fin.readline().split() - self.z=numpy.arange(self.start[0],self.end[0],(self.end[0]-self.start[0])/numindexes[0]) - if len(self.start)>1: - self.y=numpy.arange(self.start[1],self.end[1],(self.end[1]-self.start[1])/numindexes[1]) - if len(self.start)>2: - self.x=numpy.arange(self.start[2],self.end[2],(self.end[2]-self.start[2])/(numindexes[2])) + self.z = numpy.arange(self.start[0], self.end[0], (self.end[0] - self.start[0]) / numindexes[0]) + if len(self.start) > 1: + self.y = numpy.arange(self.start[1], self.end[1], (self.end[1] - self.start[1]) / numindexes[1]) + if len(self.start) > 2: + self.x = numpy.arange(self.start[2], self.end[2], (self.end[2] - self.start[2]) / (numindexes[2])) - self.norm=float(l[0]) - self.map=numpy.loadtxt(fin).reshape(numindexes) + self.norm = float(l[0]) + self.map = numpy.loadtxt(fin).reshape(numindexes) def savemap(self, filename): - fout=open(filename,'w') - for n,s in zip(self.map.shape,self.size): - fout.write('{} {}\n'.format(n-1,s)) + fout = open(filename, 'w') + for n, s in zip(self.map.shape, self.size): + fout.write('{} {}\n'.format(n - 1, s)) fout.write('{}\n'.format(self.norm)) - l=1 + totmapshape = 1 for i in self.map.shape: - l*=i - data=self.map.reshape(l) + totmapshape *= i + data = self.map.reshape(totmapshape) for j in data: fout.write('{}\n'.format(j)) - -- GitLab