-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscFuns.py~
executable file
·274 lines (237 loc) · 8.45 KB
/
MiscFuns.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
import numpy as np
from BootTest import BootStrap1
import operator as op
import functools as ft
import os, errno
import sys
from copy import deepcopy
from collections import OrderedDict
import pylab as pl
import time,datetime
from copy import deepcopy
def DelDubs(listin):
listout = []
for il in listin:
if il not in listout:
listout.append(il)
return listout
def chunks(l, n):
n = max(1, n)
return [l[i:i + n] for i in range(0, len(l), n)]
def BootNdimDict(dictin):
if isinstance(dictin,dict):
for dictkey,dictval in dictin.iteritems():
if 'Boot' in dictkey:
if isinstance(dictin,BootStrap1):
dictval.Stats()
else:
for idict in dictval:
idict.Stats()
else:
BootNdimDict(dictval)
def CheckDict(thisdict,*dictkeys):
dictcheck = deepcopy(thisdict)
for idict in dictkeys:
if idict in dictcheck.keys():
dictcheck = dictcheck[idict]
else:
return False
return True
def GetPercent(counter,totlen):
return str(int((counter*100)/float(totlen))) + '% '
def GetTimeStr(thistime):
return str(datetime.timedelta(seconds=thistime)) + ' h:m:s '
def GetTimeLeft(counter,totlen,timedone):
perdone = counter/float(totlen)
if perdone < .01:
return float(0)
else:
return timedone*((1-perdone)/perdone)
def GetTimeLeftStr(counter,totlen,timedone):
return GetTimeStr(GetTimeLeft(counter,totlen,timedone))
class Unbuffered(object):
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
sys.stdout = Unbuffered(sys.stdout)
def ParInput(constargs,pararg):
return [constargs+(ipar,) for ipar in pararg]
def ParInputTwo(constargs,pararg,pararg2):
return [constargs+(ipar,ipar2) for ipar,ipar2 in zip(pararg,pararg2)]
def ParInputTwo(constargs,pararg,pararg2,pararg3):
return [constargs+(ipar,ipar2,ipar3) for ipar,ipar2,ipar3 in zip(pararg,pararg2,pararg3)]
def touch(fname):
try:
os.utime(fname, None)
except:
open(fname, 'a').close()
def removekey(d, key):
r = dict(d)
del r[key]
return r
def VecDelta(tvec,val):
return map(int,val==np.array(tvec))
#list a is outer loop
def Elongate(lista,listb):
c = []
for ia in lista:
for ib in listb:
c.append([ia,ib])
return c
def ElongateName(lista,listb):
c = []
for ia in lista:
for ib in listb:
c.append(ia+ib)
return c
def FindWhichTSF(Title):
if 'TSinkRed' in Title or 'Reduced' in Title:
return 'test32'
elif 'TSink' in Title or 'Tsink' in Title:
return 'Tsink'
elif 'MassCM' in Title:
return 'CM'
elif 'Mass' in Title:
if 'Comparison' in Title:
return 'CM'
else:
return 'Tsink'
elif 'CM' in Title or 'Smearing' in Title:
return 'CM'
elif 'Small' in Title:
return 'Small'
else:
return 'CM'
def DelConf(data,iconf):
for delism,p in enumerate(data):
for deljsm,q in enumerate(p):
del data[delism][deljsm][iconf]
return data
def get_delcmap(thiscmap,points,delcol):
colorrange = pl.linspace(0,1,points)
midcr = (delcol[0]+delcol[1])/2.0
widcr = (delcol[1]-delcol[0])/2.0
for i,icr in enumerate(colorrange):
if icr < midcr: colorrange[i] = icr - (icr*widcr)/midcr
elif icr > midcr: colorrange[i] = (1-(widcr/(1-midcr)))*(icr-midcr) + midcr + widcr
return pl.get_cmap(thiscmap)(colorrange)
def DiagSmear(data2pt):
data2ptout = []
for ism,ismdata in enumerate(data2pt):
data2ptout.append(ismdata[ism])
return np.array(data2ptout)
def Diag3ptSmear(data3pt):
data3ptout = []
for itsink,tsinkdata in enumerate(data3pt):
data3ptout.append([])
for ism,ismdata in enumerate(tsinkdata):
data3ptout[itsink].append(ismdata[ism])
return np.array(data3ptout)
def GetBootStats(data):
flatdata = np.array(data).flatten()
map(lambda x : x.Stats() ,flatdata)
return np.reshape(flatdata,np.array(data).shape)
def MassFun(cfun,Dt=1):
mass = []
for it,tcfun in enumerate(cfun):
if it+Dt < len(cfun):
mass.append(np.abs(np.log(np.abs(cfun[it+Dt]/tcfun)))/Dt)
return GetBootStats(mass)
def cfunTOmass(cfun):
if len(np.array(cfun).shape) > 1:
out = np.array(NDimOpp(cfun,1,MassFun))
return np.rollaxis(out,0,len(out.shape))
else:
return MassFun(cfun)
def flattenAllBut(data,dimleft):
data = np.array(data)
return data.reshape(reduce(op.mul,data.shape[:-dimleft]), *data.shape[-dimleft:])
def MultArray(data):
return ft.reduce(op.mul, list(data), 1)
##applies funin datavariables over all - dimleft variables with extra variables funvars using funin
def NDimOpp(data,dimleft,funin,*funvars):
npdata = np.array(data)
dataout = np.array([])
if dimleft > 0:flatdata = flattenAllBut(npdata,dimleft)
else: flatdata = npdata.flatten()
for idata in flatdata:
dataapp = funin(idata,*funvars)
datainshape = np.array(dataapp).shape
dataout = np.append(dataout,dataapp)
if dimleft > 0: outdim = npdata.shape[:-dimleft] + datainshape
else: outdim = npdata.shape
dataout = np.reshape(dataout,outdim)
for roll in datainshape:
dataout = np.rollaxis(dataout,len(dataout.shape)-1)
return dataout
def NDimOpp2(data,data2,dimleft,funin,*funvars):
npdata,npdata2 = np.array(data),np.array(data2)
dataout = np.array([])
if dimleft > 0:flatdata = zip(flattenAllBut(npdata,dimleft),flattenAllBut(npdata2,dimleft))
else: flatdata = zip(npdata.flatten(),npdata2.flatten())
for idata,idata2 in flatdata:
dataapp = funin(idata,idata2,*funvars)
datainshape = np.array(dataapp).shape
dataout = np.append(dataout,dataapp)
if dimleft > 0: outdim = npdata.shape[:-dimleft] + datainshape
else: outdim = npdata.shape
if MultArray(dataout.shape) > MultArray(outdim): extra = (MultArray(dataout.shape)/MultArray(outdim),)
else : extra = ()
dataout = np.reshape(dataout,outdim)
for roll in datainshape:
dataout = np.rollaxis(dataout,len(dataout.shape)-1)
return dataout
def NDimOpp3(data,data2,data3,dimleft,funin,*funvars):
npdata,npdata2,npdata3 = np.array(data),np.array(data2),np.array(data3)
dataout = np.array([])
if dimleft > 0:flatdata = zip(flattenAllBut(npdata,dimleft),flattenAllBut(npdata2,dimleft),flattenAllBut(npdata3,dimleft))
else: flatdata = zip(npdata.flatten(),npdata2.flatten(),npdata3.flatten())
for idata,idata2,idata3 in flatdata:
dataapp = funin(idata,idata2,idata3,*funvars)
datainshape = np.array(dataapp).shape
dataout = np.append(dataout,dataapp)
if dimleft > 0: outdim = npdata.shape[:-dimleft] + datainshape
else: outdim = npdata.shape
if MultArray(dataout.shape) > MultArray(outdim): extra = (MultArray(dataout.shape)/MultArray(outdim),)
else : extra = ()
dataout = np.reshape(dataout,outdim)
for roll in datainshape:
dataout = np.rollaxis(dataout,len(dataout.shape)-1)
return dataout
#Pulls out a class "atribute" out of a numpy tensor "data" of classes ##
#e.g. return data[:,:,:].atribute
def Pullflag(data,atribute):
npdata = np.array(data)
dataout = np.array([])
for idata in npdata.flatten():
flagdim = np.array(getattr(idata,atribute)).shape
dataout = np.append(dataout,getattr(idata,atribute))
return np.reshape(dataout,npdata.shape + flagdim)
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
def SplitList(thelist):
return [ilist[0] for ilist in thelist],[itlist[1] for ilist in thelist]
def UDIndex(gammalist):
pairindex,newgammalist = [],deepcopy(gammalist)
for dindex,dgamma in enumerate(gammalist):
if 'doub' in dgamma:
for uindex,ugamma in enumerate(gammalist):
if dgamma.replace('doub','sing') == ugamma:
pairindex.append([dindex,uindex])
newgammalist.append(dgamma.replace('doub',''))
return newgammalist,pairindex
## USED FOR OLD OPPERATOR DIRECTORIES##
# if kappa == 12090:
# def CreateOppDir(Opp):
# return Opp
# else: