forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
246 lines (205 loc) · 9.77 KB
/
Main.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
#-------------------------------------------------------------------------------
# Name: Plotting absorbance curves
# Purpose: Analysing experiments's results
#
# Author: Alessandro Volpato
#
# Created: 25/07/2018
# Copyright:
# Licence: CC BY-SA 4.0
#-------------------------------------------------------------------------------
#import io,sys
import math
import matplotlib.image as img
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
######## INSERT HERE THE FILE NAME ################
# #
# NOTE: Check the format!! png or csv #
referenceFile = "IPA_Glass.png"
samples = ["1_IPA_Glass.png","2_IPA_Glass.png"]
plotDirectly = []
title = "Chlorophyll A and B in isopropilic alcohol"
plotColors = ["purple","darkgreen"]
patchLabels = ["Chlorophyll A","Chlorophyll B"]
saveFilename = "plot.png"
# Don't change them!!
#
###################################################
###### CALIBRATION HERE ###########################
###### INSERT HERE YOUR PIXEL CORRELATIONS ########
#
pixel = [115, 146, 193, 250, 312, 329, 404]
wavelength = [405.4,436.6,487.7,546.5,611.6,631.1,708]
#
###################################################
####################################################################
#
# This is a function. Each time we call getSpectrum_PNG #
# for the filename in brackets, we will execute these operations #
#
def getSpectrum_PNG(filename): #
'''From a PNG file taken with spectralworkbench
extracts a spectrum. Each channel's spectrum
is calculated as column mean for the whole picture''' #
#
# Reading the image #
print("Reading image") #
image = img.imread(filename) #
#
# Preparing the variables #
imageR = [] #
imageG = [] #
imageB = [] #
imgWidth = len(image[0]) #
imgHeight = len(image) #
#
# Preparing the RGB arrays #
for i in range(imgWidth): #
imageR.append(image[0][i][0]) #
imageG.append(image[0][i][1]) #
imageB.append(image[0][i][2]) #
#
# Columns summatory #
for i in range(imgHeight): #
for j in range(imgWidth): #
imageR[j]=imageR[j]+image[i][j][0] #
imageG[j]=imageG[j]+image[i][j][1] #
imageB[j]=imageB[j]+image[i][j][2] #
#
# Calculating the mean for every RGB column #
for i in range(imgWidth): #
imageR[i]=imageR[i]/imgHeight #
imageG[i]=imageG[i]/imgHeight #
imageB[i]=imageB[i]/imgHeight #
#
# Merging the RGB channels by addition #
spectrum = [] #
for i in range(imgWidth): #
spectrum.append((imageR[i]+imageG[i]+imageB[i])/3) #
#
# returning the results of the operation #
return spectrum #
#
def getSpectrum_CSV(filename): #
'''From a CSV file containing the serie of measurements,
splits the values and returns them as a list, the spectrum'''
#
# Reading the file #
print("Reading csv file") #
inFile = open(filename, "r") #
CSVline = inFile.read() #
#
# Splitting the values #
spectrumSTR = CSVline.split(",") #
#
# Transforming the values from string to float type #
spectrum = [] #
for i in range(len(spectrumSTR)): #
spectrum.append(float(spectrumSTR[i])) #
#
# Returning the results of the operation #
return spectrum #
#
# ----------- Function ----------------------------
def normalise(spectrumIn):
spectrumOut = []
maxPoint = max(spectrumIn)
for value in spectrumIn:
spectrumOut.append(value/maxPoint)
return spectrumOut
def calcAbs(reference, sample):
# Calculate transmittance and absorbance spectrum
transmittance = []
absorbance = []
for i in range(len(reference)):
if sample[i] == 0: # This 'if' part is to aqvoid math error due to 0/number
transmittance.append(0)
absorbance.append(0) # Conceptually wrong, if sample > reference, artificious data distortion has happened
else:
transmittance.append(sample[i]/reference[i])
absorbance.append(-math.log(transmittance[i],10)/5)
return absorbance
####################################################################
# A list of colors to use in python
colors = ['indigo' , 'gold' , 'hotpink' , 'firebrick' , 'indianred' , 'yellow' ,
'mistyrose' , 'darkolivegreen' , 'olive' , 'darkseagreen' , 'pink' , 'tomato' ,
'lightcoral' , 'orangered' , 'navajowhite' , 'lime' , 'palegreen' , 'darkslategrey' ,
'greenyellow' , 'burlywood' , 'seashell' , 'mediumspringgreen' , 'fuchsia' ,
'papayawhip' , 'blanchedalmond' , 'chartreuse' , 'dimgray' , 'black' , 'peachpuff' ,
'springgreen' , 'aquamarine' , 'orange' , 'lightsalmon' , 'darkslategray' ,
'brown' , 'ivory , dodgerblue' , 'peru' , 'lawngreen' , 'chocolate' , 'crimson' ,
'forestgreen' , 'darkgrey' , 'lightseagreen' , 'cyan' , 'mintcream' , 'silver' ,
'antiquewhite' , 'mediumorchid' , 'skyblue' , 'gray' , 'darkturquoise' , 'goldenrod' ,
'darkgreen' , 'floralwhite' , 'darkviolet' , 'darkgray' , 'moccasin , saddlebrown' ,
'grey' , 'darkslateblue' , 'lightskyblue' , 'lightpink , mediumvioletred' ,
'slategrey' , 'red' , 'deeppink' , 'limegreen' , 'darkmagenta' , 'palegoldenrod' ,
'plum' , 'turquoise' , 'lightgrey' , 'lightgoldenrodyellow' , 'darkgoldenrod' ,
'lavender' , 'maroon' , 'yellowgreen' , 'sandybrown' , 'thistle' , 'violet' , 'navy' ,
'magenta' , 'dimgrey' , 'tan' , 'rosybrown' , 'olivedrab' , 'blue' , 'lightblue' ,
'ghostwhite' , 'honeydew' , 'cornflowerblue' , 'slateblue' , 'linen' , 'darkblue' ,
'powderblue' , 'seagreen' , 'darkkhaki' , 'snow' , 'sienna' , 'mediumblue' , 'royalblue' ,
'lightcyan' , 'green' , 'mediumpurple' , 'midnightblue' , 'cornsilk' , 'paleturquoise' ,
'bisque' , 'slategray' , 'darkcyan' , 'khaki' , 'wheat' , 'teal' , 'darkorchid' , 'salmon' ,
'deepskyblue' , 'rebeccapurple' , 'darkred' , 'steelblue' , 'palevioletred' ,
'lightslategray' , 'aliceblue' , 'lightslategrey' , 'lightgreen' , 'orchid' ,
'gainsboro' , 'mediumseagreen' , 'lightgray' , 'mediumturquoise' , 'lemonchiffon' ,
'cadetblue , lightyellow , lavenderblush , coral , purple' , 'aqua' ,
'mediumslateblue' , 'darkorange' , 'mediumaquamarine' , 'darksalmon' , 'beige' ,
'blueviolet' , 'azure' , 'lightsteelblue' , 'oldlace']
# Preparing the plot
# Initialize and load spectra
reference = getSpectrum_PNG(referenceFile)
# Samples
samplesSpectra = []
for filenName in samples:
samplesSpectra.append(getSpectrum_PNG(filenName))
absorbances = []
for spectrum in samplesSpectra:
absorbances.append(normalise(calcAbs(reference, spectrum)))
# Plot without calculations
plotDirectlySpectra = []
for filenName in plotDirectly:
plotDirectlySpectra.append((getSpectrum_PNG(filenName)))
PDirect = []
for spectrum in plotDirectlySpectra:
PDirect.append(normalise(spectrum))
spectraToPlot = absorbances + PDirect
# Finding out the coefficients
params = np.polyfit(pixel,wavelength,3)
#return p = np.poly1d(range)
# Solving the equation for every pixel
# (Assigning to every pixel a wavelength)
nmAxis = []
for i in range(len(spectrum)):
v1 = params[0]*float(i**3)
v2 = params[1]*float(i**2)
v3 = params[2]*float(i**1)
v4 = params[3]*float(i**0)
nmAxis.append(v1+v2+v3+v4)
# NOTE: This operation is quickly done with the later used:
# nmAxis = np.poly1d(len(spectrum))
# print(len(patchLabels), len(spectraToPlot))
if len(patchLabels) < len(spectraToPlot):
patchLabels.append([""]*(len(spectraToPlot)-len(patchLabels)))
if len(plotColors) < len(spectraToPlot):
plotColors = []
for i in range(len(spectraToPlot)):
plotColors.append(colors[i])
patches = []
for i in range(len(patchLabels)):
patches.append(mpatches.Patch(color=plotColors[i], label=patchLabels[i]))
colorCounter = 0
for spectrum in spectraToPlot:
plt.plot(nmAxis, spectrum, color = plotColors[colorCounter])
colorCounter += 1
plt.title(title)
plt.legend(handles=patches)
plt.xlim(350, 950)
plt.ylim(-0.25,2)
plt.xlabel('Wavelegth (nm)')
plt.ylabel('Absorbance')
plt.savefig(saveFilename)
plt.show()
# END --------------------------------------------------------