-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrank_prob.py
More file actions
312 lines (270 loc) · 10 KB
/
rank_prob.py
File metadata and controls
312 lines (270 loc) · 10 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#import sys
import os.path
#from os.path import basename
#import re
import pandas as pd #para imprimir en forma de tabla
from myLibs.miscellaneus import WriteOutputFileRR
from math import sqrt
#from matplotlib import pyplot as plt
#import numpy as np
#from scipy.optimize import curve_fit
#from scipy import asarray as ar,exp
#from math import sqrt, pi
#import time
#import signal
#import keyboard
# mainPath=sys.path[0] # sources dir
from myLibs.parsers import getDictFromInfoFile, getMyFileDictRankAdv, functionDictAdv, findRangeInfoDict, isValidSpecFile
from myLibs.miscellaneus import getIdxRangeVals, removeDuplicates
#from myLibs.gilmoreStats import *
#from myLibs.fitting import *
#from myLibs.autoPeakFunk import *
from myLibs.QueryDB import OpenDatabase, CloseDatabase, EnergyRange, halfLifeUnit, GetIntensities
from myLibs.fitting import doFittingStuff,MeanDistance
from myLibs.gilmoreStats import doGilmoreStuff
from operator import itemgetter
#from myLibs.plotting import *
def rankProb(ListOpt):
List = ListOpt.copy()
List.pop(0)
i = 0 #for rank op
rankOp = []
rankOp2 = [None,None,None]
if '--noCal' in List:
noCalFlag = True
List.remove('--noCal')
else:
noCalFlag = False
if '--wof' in List:
wofFlag = True
List.remove('--wof')
else:
wofFlag = False
if '--all' in List:
allFlag = True
List.remove('--all')
else:
allFlag = False
for Arg in List:
try:
if int(Arg) > 0 and int(Arg) < 4:
rankOp.append(int(Arg))
else:
continue
except:
if len(List) <= 1:
rankOp.append(3)
continue
if len(rankOp) == 0:
rankOp.append(3)
rankOp.append(2)
elif len(rankOp) == 1:
if rankOp[0] == 3:
rankOp.append(2)
else:
rankOp.append(3)
rankOp = removeDuplicates(rankOp)
#rankOp2 = [x-4 for x in rankOp]
rankOp2 = [-2,-3]
if len(List) == 0:
print("error: --rank option needs an argument")
return 0
infoFile=List[0]
if not os.path.isfile(infoFile):
print("error: %s does not exist, are you in the right path?" %(infoFile))
return 10000
if not infoFile.endswith('.info'):
print("error: %s needs a .info extension" % (infoFile))
return 10001
infoDict=getDictFromInfoFile(infoFile)
for arg in List:
if isValidSpecFile(arg):
if arg.endswith('.info'):
pass#infoFile = arg
else:
FileName = arg
try:
if isValidSpecFile(FileName):
FileExt = FileName.split('.')[-1]
except:
print('ERROR: Unexpected error. Not a valid file used.')
return 110
if noCalFlag:
FileDict = functionDictAdv[FileExt](FileName,False)
else:
FileDict = functionDictAdv[FileExt](FileName)
try:
minRange = infoDict['Range']['start']
maxRange = infoDict['Range']['end']
del infoDict['Range']
except:
minRange, maxRange = findRangeInfoDict(infoDict)
idxPairL = []
for DictEle in infoDict.values():
idxPairL.append([DictEle['start'],DictEle['end']])
DBInfoL = []
pathfile = os.path.realpath(__file__)
pathfile = pathfile.rstrip('rank_prob.py')
conexion = OpenDatabase(pathfile)
memoLenDict={}
isoCountD = {}
DBInfoL = []
DBInfoDL = []
tMinEL = []
tMaxEL = []
ProbLL = []
DiffLL = []
ProbDL = {}
DiffDL = {}
if True:
for infoPair in infoDict.values():
tMinEL.append(infoPair['start'])
tMaxEL.append(infoPair['end'])
tMinE = min(tMinEL)
tMaxE = max(tMaxEL)
else:
tMinE = minRange
tMaxE = maxRange
myDataList = FileDict['theList']
#print("")
#print("Gilmore statistics\n[variables in counts]")
fittingDict=doFittingStuff(infoDict,myDataList)
#gaussData4Print=[]
#fig, ax = plt.subplots()
#for e in fittingDict:
#a,mean,sigma,c,minIdx,maxIdx,myFWHM=fittingDict[e]
# a,mean,sigma,c=fittingDict[e][:-3]
# if a == None:
# print("Skipping failed fit")
# continue
# gaussData4Print.append([e,a,mean,sigma,c])
#plt.annotate(e, xy=[mean,a])
#myGaussRows=['#tags','a','mean','sigma','c']
#pd.set_option('display.max_rows', None)
#dfG = pd.DataFrame(gaussData4Print, columns = myGaussRows)
#gilmoreDict=doGilmoreStuff(infoDict,myDataList)
#data4print=[]
#for e in gilmoreDict:
# gL=gilmoreDict[e]
# data4print.append(gL[0:6])
#realXVals=myDataList[0]
#myHStr4=['Tags','NetArea','Area+ExtBkgd','GrossInt','Background','Sigma_A']
#pd.set_option('display.max_rows', len(data4print))#imprime todas las filas
#df = pd.DataFrame([data for data in data4print], columns = myHStr4)
#print(df)
#print('\nGauss Parameters')
#print(dfG)
fittingDictKeys = list(fittingDict.keys())
PeakNum = -1
IdxRemove=[]
#idxPairL_copy=idxPairL.copy()
for idxR in idxPairL:
PeakNum += 1
iEner = idxR[0]
fEner = idxR[1]
# DBInfoL.append(GetIntensities(conexion,iEner,fEner))
# DBInfo = DBInfoL[-1]
DBInfo = GetIntensities(conexion,iEner,fEner)
DiffL, ProbL = MeanDistance(DBInfo,fittingDict[fittingDictKeys[PeakNum]])
if DiffL == None or ProbL == None:
continue
else:
IdxRemove.append(PeakNum)
DBInfoL.append(DBInfo)
DiffLL.append(DiffL)
ProbLL.append(ProbL)
DBInfoD = {}
for e,fs,gs in zip(DBInfo,DiffL,ProbL):
if e[-1] not in DBInfoD:
DBInfoD[e[-1]] = [e]
ProbDL[gs[0]] = [gs]
DiffDL[fs[0]] = [fs]
else:
DBInfoD[e[-1]].append(e)
ProbDL[gs[0]].append(gs)
DiffDL[fs[0]].append(fs)
DBInfoDL.append(DBInfoD)
for Ele in DBInfo:
iso = Ele[-1]
if iso not in memoLenDict:
memoLenDict[iso]=[len(GetIntensities(conexion,tMinE,tMaxE,iso)),1,Ele[10],[PeakNum]]
isoCountD[iso] = [Ele]
else:
memoLenDict[iso][1] += 1
memoLenDict[iso][2] += Ele[10]
memoLenDict[iso][3].append(PeakNum)
isoCountD[iso].append(Ele)
idxPairL=list(map(idxPairL.__getitem__,IdxRemove))
memoLenDictKeys = memoLenDict.copy().keys()
for Ele in memoLenDictKeys:
if memoLenDict[Ele][0] == 0 or memoLenDict[Ele][2] == 0:
del memoLenDict[Ele]
del isoCountD[Ele]
for DBInfoD in DBInfoDL:
try:
del DBInfoD[Ele]
except KeyError:
continue
memoLenDictKeys = memoLenDict.keys()
Ranges = []
rankList = []
try:
if rankOp[0] == 1:
myfilename = infoFile.strip('.info') + '_rank_B.txt'
elif rankOp[0] == 2:
myfilename = infoFile.strip('.info') + '_rank_C.txt'
elif rankOp[0] == 3:
myfilename = infoFile.strip('.info') + '_rank_Prob.txt'
else:
myfilename = infoFile.strip('.info') + '_rank_Prob.txt'
except:
myfilename = 'FileNameCouldNotBeRecovered.txt'
for idxR,DBInfoD in zip(idxPairL,DBInfoDL):
iEner = idxR[0]
fEner = idxR[1]
Ranges.append([iEner,fEner])
Eg , Ig , Decay, Half , Parent, rank, rank2, rank3, ProbRank, DiffRank = [],[],[],[],[],[],[],[],[],[]
for Key in DBInfoD:
#Ele = DBInfoD[Key]
for Ele,DiffEle,ProbEle in zip(DBInfoD[Key],DiffDL[Key],ProbDL[Key]):
Eg.append(Ele[1])
Ig.append(round(Ele[3],2))
Decay.append(Ele[5])
x=halfLifeUnit(Ele)
if x == 0:
y = str(x)
else:
y = str('{0:.2e}'.format(x))
Half.append(y+ ' [s] ')
Parent.append(Ele[-1])
rank.append(memoLenDict[Ele[-1]][1])
rank2.append(round(memoLenDict[Ele[-1]][1]/memoLenDict[Ele[-1]][0],3))
rank3.append(round(memoLenDict[Ele[-1]][2],3))
ProbRank.append(ProbEle[1])
DiffRank.append(DiffEle[1])
Eg,Ig,Decay,Half,Parent,rank3,ProbRank,DiffRank = (list(t) for t in zip(*sorted(zip(Eg,Ig,Decay,Half,Parent,rank3,ProbRank,DiffRank),key=itemgetter(*rankOp2) ,reverse=True)))
print('\nThe energy range consulted is between %.2f keV and %.2f keV.\n' % (iEner,fEner))
if allFlag:
pd.set_option('display.max_rows', None) #imprime todas las filas
pd.options.display.float_format = '{:,.5f}'.format
df = pd.DataFrame(list(zip(Eg,Ig,Decay,Half,Parent,rank3,ProbRank,DiffRank)),columns=['Eg [keV]','Ig (%)','Decay m','Half Life','Parent','Rank3','Probability','Distance'])#crea la tabla
print(df)
else:
pd.set_option('display.max_rows', len(Ele))
df = pd.DataFrame(list(zip(Eg,Ig,Decay,Half,Parent,rank3,ProbRank,DiffRank)),columns=['Eg [keV]','Ig (%)','Decay mode','Half Life','Parent','Rank3','Probability','Distance'])#crea la tabla
print(df)
if wofFlag:
try:
if allFlag:
WriteOutputFileRR(myfilename,df,iEner,fEner)
else:
WriteOutputFileRR(myfilename,df.head(10),iEner,fEner)
except IOError:
print('ERROR: An unexpected error ocurrs. Data could not be saved.')
break
if wofFlag:
print('-----------------------------------------')
print('The file was saved as:')
print(myfilename)
print('-----------------------------------------')
return 0