-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectoPlot.py
More file actions
217 lines (182 loc) · 7.83 KB
/
Copy pathReflectoPlot.py
File metadata and controls
217 lines (182 loc) · 7.83 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
# coding: utf-8
# In[1]:
import matplotlib as mpl
mpl.use("pgf")
import matplotlib.pyplot
import numpy as np
import scipy.interpolate as inter
import copy
import sys
import os
import string
import warnings
import functools
import copy
import inspect
from matplotlib import rc
from Filereader import fileToNpArray
from Data import Data
from Fitter import Fitter
from Plot import Plot
# In[2]:
class ReflectoPlot(Plot):
#NaturKonst
e=1.6*10**-19 #C
K_m=683 #lm/W
c=2.99*10**17 #nm/s
h=6.63*10**-34 #J*s
#ProgKonst
chars=list(string.ascii_uppercase) #alphabetUppercase
convFac=(h*c)/e #eV*nm
@classmethod
def wavelengthToEV(cls,wavelength,intens):
energy=wavelength**-1*cls.convFac #eV
corFac=wavelength**2*cls.e/(cls.h*cls.c) #nm/eV
intensEnergy=intens*corFac #W/(sr*m^2*eV)
return (energy,intensEnergy)
@classmethod
def noNegatives(cls,a):
return np.maximum(a,np.zeros(len(a), dtype=np.float64))
@classmethod
def normalize(cls,a):
b=a-np.amin(a)
return b/np.amax(b, axis=0)
@classmethod
def gauss(cls, x, mu, amp, sigma):
return amp/(np.sqrt(2*np.pi*sigma**2))*np.exp(-((x-mu)**2/(2*sigma**2)))
@classmethod
def twoGaussSplines(cls, x, mu, amp, sigma, sigma2):
return amp*np.exp(-((x-mu)**2/(2*sigma**2)))*np.heaviside(x-mu,0)+amp*np.exp(-((x-mu)**2/(2*sigma2**2)))*np.heaviside(mu-x,0)
def __init__(self,
name,
fileListRefl,
fileListTrans,
fileFormat={"separator":"\t", "skiplines":0},
fileFormat2=None,
title=None,
showTrans=True,
showRefl=True,
showAbs=False,
showColAxType=["lin","lin","lin","lin","lin","lin","lin","lin","lin"],
showColAxLim=[None,None,None,None,None,None,None,None,None],
showColLabel= ["","Wavelength","Reflection", "Transmission", "Absorption", "Energy","Transmission", "Reflection","Absorption"],
showColLabelUnit=["","Wavelength (nm)","Transmission","Reflection","Absorption","Energy (eV)","Normalized Transmission", "Normalized Reflection","Normalized Absorption"],
averageMedian=False,
errors=False,
formatAbs="-",
formatTrans="--",
formatRefl=":",
reflAlpha = 0.3,
transAlpha = 0.3,
**kwargs
):
Plot.__init__(self, name, [[trans,refl] for trans,refl in zip(fileListTrans,fileListRefl)], averageMedian=averageMedian, showColAxType=showColAxType, showColAxLim=showColAxLim, showColLabel=showColLabel, showColLabelUnit=showColLabelUnit, fileFormat=fileFormat, errors=errors, **kwargs)
#dyn inits
if title is None:
self.title=name
else:
self.title=title
if fileFormat2 is None:
self.fileFormat2=fileFormat
else:
self.fileFormat2=fileFormat2
self.fileListTrans=fileListTrans
self.fileListRefl=fileListRefl
self.labelsOrig=self.labels
self.labels=self.labels#[l +" "+self.showColLabel[self.showCol] for l in self.labels]
self.showTrans=showTrans
self.showRefl=showRefl
self.showAbs=showAbs
self.formatAbs=formatAbs
self.formatTrans=formatTrans
self.formatRefl=formatRefl
self.transLs=formatTrans
self.reflLs=formatRefl
self.reflAlpha = reflAlpha
self.transAlpha = transAlpha
self.dataList=self.importData()
@functools.lru_cache(maxsize=None)
def importData(self):
dataList=[]
for reflData, transData in zip(self.fileListRefl,self.fileListTrans):
a=Data(fileToNpArray(reflData, **self.fileFormat)[0])
b=Data(fileToNpArray(transData, **self.fileFormat2)[0])
x=a.getSplitData2D()[0]
refl=a.getSplitData2D()[1]
trans=b.getSplitData2D()[1]
absorp=[1]*len(trans)-trans-refl
energy,e_refl=self.wavelengthToEV(x, refl)
e_trans=self.wavelengthToEV(x, refl)[1]
e_absorp=[1]*len(e_trans)-e_trans-e_refl
e_refl=self.normalize(e_refl)
e_trans=self.normalize(e_trans)
e_absorp=self.normalize(e_absorp)
data=Data.mergeData([x,refl,trans,absorp,energy,e_refl,e_trans,e_absorp])
dataList.append([Data(data)])
return dataList
def processFileName(self, option=".pdf"):
if self.filename is None:
string=self.name.replace(" ","")+self.fill+"abs_spectra"
else:
string=self.filename
if not self.scaleX is 1:
string+=self.fill+"scaledWith{:03.0f}Pct".format(self.scaleX*100)
if not self.filenamePrefix is None:
string=self.filenamePrefix+self.fill+string
return string+option
#def processData(self):
#return self.dataList
def rect(self,x,y,w,h,c):
polygon = matplotlib.pyplot.Rectangle((x,y),w,h,color=c)
self.ax.add_patch(polygon)
def rainbow_fill(self,X,Y, cmap=matplotlib.pyplot.get_cmap("nipy_spectral")):
dx = X[1]-X[0]
S = 380
N = 675
h= 0.01
for n, (x,y) in enumerate(zip(X,Y)):
if (x>N):
color= cmap(0.9999)
elif (x<S):
color= cmap(0)
else:
color = cmap((x-S)/(N-S))
self.rect(x,-0.035,dx,h,color)
def xColTicksToXCol2Ticks(self, ticks):
if self.xCol==1 and self.xCol2==5:
ticks=ticks**-1*self.convFac
ticks=np.around(ticks,decimals=1)
return ["{:2.1f}".format(tick) for tick in ticks]
elif self.xCol==5 and self.xCol2==1:
ticks=ticks**-1*self.convFac
ticks=np.around(ticks,decimals=0)
return ["{:3.0f}".format(tick) for tick in ticks]
else:
return ticks
def afterPlot(self):
ax=self.ax
labelsOrig=self.labelsOrig
xCol=self.xCol
colors=self.colors
for n in range(0,len(self.expectData)):
if self.xCol==1:
self.rainbow_fill(*self.expectData[0].getSplitData2D())
if self.showTrans:
ax.errorbar(*self.expectData[n].getSplitData2D(xCol=xCol, yCol=2), c=colors[n], ls=self.transLs, alpha=self.transAlpha, label=labelsOrig[n]+" "+self.showColLabel[2])
elif self.xCol==5:
if self.showTrans:
ax.errorbar(*self.expectData[n].getSplitData2D(xCol=xCol, yCol=6), c=colors[n], ls=self.transLs, alpha=self.transAlpha, label=labelsOrig[n]+" "+self.showColLabel[6])
for n in range(0,len(self.expectData)):
if self.xCol==1:
if self.showRefl:
ax.errorbar(*self.expectData[n].getSplitData2D(xCol=xCol, yCol=3), c=colors[n], ls=self.reflLs, alpha=self.reflAlpha, label=labelsOrig[n]+" "+self.showColLabel[3])
elif self.xCol==5:
if self.showRefl:
ax.errorbar(*self.expectData[n].getSplitData2D(xCol=xCol, yCol=7), c=colors[n], ls=self.reflLs, alpha=self.reflAlpha, label=labelsOrig[n]+" "+self.showColLabel[7])
for n in range(0,len(self.expectData)):
if self.xCol==1:
if self.showAbs:
ax.errorbar(*self.expectData[n].getSplitData2D(xCol=xCol, yCol=4), c=colors[n], ls=self.ls, label=labelsOrig[n]+" "+self.showColLabel[4])
elif self.xCol==5:
if self.showAbs:
ax.errorbar(*self.expectData[n].getSplitData2D(xCol=xCol, yCol=8), c=colors[n], ls=self.ls, label=labelsOrig[n]+" "+self.showColLabel[8])