Repository navigation
Expand file tree
/
Copy pathFilmDoseClass.py
More file actions
534 lines (472 loc) · 23.1 KB
/
Copy pathFilmDoseClass.py
File metadata and controls
534 lines (472 loc) · 23.1 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import ntpath
import numpy as np
from lmfit import Model
import tifffile #https://pypi.org/project/tifffile/
from PIL import Image
import FilmCalibrationInterp
import matplotlib.pyplot as plt
import matplotlib.widgets as widgets
import tkinter as tk
from tkinter import simpledialog
from tkinter import filedialog
import os
import ctypes
import json
class FilmDose:
def __init__(self, filmfile):
self.workingdir = ntpath.dirname(filmfile) + '\\'
self.imagefilename = ntpath.basename(filmfile)
image = tifffile.imread(filmfile)
self.PILImage = Image.open(filmfile)
tif = tifffile.TiffFile(filmfile)
self.tif = tif
# Get dpi and pixel size
self.dpi = np.array([1.0, 1.0])
tag = tif.pages[0].tags['XResolution']
self.dpi[1] = tag.value[0]/tag.value[1]
tag = tif.pages[0].tags['YResolution']
self.dpi[0] = tag.value[0]/tag.value[1]
if(str(tif.pages[0].tags['ResolutionUnit']).split()[-1]=='INCH'):
self.pixelsize = 25.4/self.dpi
elif(str(tif.pages[0].tags['ResolutionUnit']).split()[-1]=='MM'):
self.dpi = self.dpi/25.4
self.pixelsize = 1/self.dpi
self.imagearray = np.array(image)
self.OD = np.log10(65535.0/self.imagearray)
self.OD0 = np.log10(np.array([65535.0, 65535.0, 65535.0]))
self.ODref = np.log10(np.array([65535.0, 65535.0, 65535.0]))
self.ROI = np.array(1)
self.Dref = np.empty(3)
self.CalibrationCorrectionFactors = np.array([1.0, 1.0, 1.0])
# Parametros para correccion de dependencia energética
self.fad = 1.0
self.kintrinsic = 1.0
def get_don(self):
nod = np.clip(self.OD - self.OD0, 0.0)
return nod
def set_don(self, don):
self.OD = don + self.OD0
self.set_imagearray_from_od(self.OD)
def get_do(self):
return self.OD
def set_do(self, do):
self.OD = do
self.set_imagearray_from_od(do)
def get_dose(self):
darray = np.zeros(self.OD.shape, dtype=np.float)
#for c in [0, 1, 2]:
# darray[:, :, c] = self.Calibration.get_dose_from_nod((self.OD[:, :, c] - self.OD0[c]), c)
darray = self.Calibration.get_dose_from_nod((self.OD - self.OD0))
# Se permiten dosis hasta un 20% superiores al máximo de la curva de calibración
darray = np.clip(darray, 0, 1.2 * np.max(self.Calibration.D))
return darray
def get_pixel(self):
return self.imagearray
def set_pixel(self, pixel):
self.imagearray = pixel
self.set_od_from_imagearray(pixel)
def set_od_from_imagearray(self, imarr):
self.imagearray = np.array(imarr)
self.OD = np.log10(65535.0 / self.imagearray)
self.ROI = np.array(1)
def set_imagearray_from_od(self, od):
self.imagearray = np.clip(65535.0 / np.power(10, self.OD), 0, 65535).astype(np.uint16)
def get_pixelheight(self):
return self.pixelsize[0]
def get_pixelwidth(self):
return self.pixelsize[1]
def set_pixelsize(self, ps):
self.pixelsize[0] = ps
self.pixelsize[1] = ps
self.dpi = 25.4 / self.pixelsize
def set_pixelheight(self,ph):
self.pixelsize[0] = ph
self.dpi[0] = 25.4 / self.pixelsize[0]
def set_pixelwidth(self,pw):
self.pixelsize[1] = pw
self.dpi[1] = 25.4 / self.pixelsize[1]
# Genera la matriz de dosis a partir de la matriz de densidad óptica y la calibración
def DoseArrays(self):
darray = np.zeros(self.OD.shape, dtype=np.float)
for c in [0, 1, 2]:
darray[:, :, c] = self.fad*self.Calibration.get_dose_from_nod((self.OD[:, :, c] - self.OD0[c]), c)
# Se permiten dosis hasta un 20% superiores al máximo de la curva de calibración
darray = np.clip(darray, 0, 1.5 * np.max(self.Calibration.D))
return darray
# Guarda la imagen en formato tiff 48-bits con valor de pixel lineal con la dosis
def SaveDoseImage(self, outputfile, Dmax):
arr = (self.DoseArrays() * 65535 / Dmax)
arr[arr > 65535] = 65535
darray = arr.astype(np.uint16)
tifffile.imwrite(outputfile, darray, resolution=(self.dpi[0], self.dpi[1]))
# Guarda la imagen corregida en Tiff 48-bits
def SaveCorrectedPVImage(self, outputfile):
PV = (65535.0 / np.power(10, self.OD)).astype(np.uint16)
tifffile.imwrite(outputfile, PV, resolution=(self.dpi[0], self.dpi[1]))
def show_Image(self):
fig = plt.figure()
plt_image = plt.imshow(self.PILImage)
plt.show()
def show_DoseImage(self, dmax):
fig = plt.figure()
plt_image = plt.imshow(self.generate_DoseImage(dmax))
plt.show()
def generate_DoseImage(self, dmax):
arr = np.clip(self.DoseArrays()*256/dmax,0,255).astype(np.uint8)
return arr
def add_calibration_objects(self, fcalobj):
self.Calibration = fcalobj
self.OD0 = self.Calibration.OD0
def add_calibration(self, pv, d, pv_std_dev=None):
fcalobj = FilmCalibrationInterp.FilmCalibration()
fcalobj.calibration_from_arrays(pv, d, pv_std_dev)
self.Calibration = fcalobj
self.OD0 = self.Calibration.OD0
# Carga una curva de calibración desde un fichero
def AddCalibrationFromFile(self, inputfile):
#root = tk.Tk()
#root.withdraw()
#file_path = filedialog.askopenfilename(title='Open Calibration file')
#root.destroy()
self.Calibration = FilmCalibrationInterp.FilmCalibration(inputfile)
self.OD0 = self.Calibration.OD0
# Función que permite seleccionar una ROI rectangular en la imagen.
# Devuelve las coordenadas de la ROI
def SelectRectangle(self):
fig = plt.figure()
ax = fig.add_subplot(111)
plt_image = plt.imshow(self.PILImage)
rs = widgets.RectangleSelector(
ax, self.__onselect, drawtype='box',
rectprops=dict(facecolor='red', edgecolor='black', alpha=0.2, fill=True))
plt.show(block=True)
x = [np.int(rs.corners[0][0]), np.int(rs.corners[0][2])]
y = [np.int(rs.corners[1][0]), np.int(rs.corners[1][2])]
return rs
# Función que captura las coordenadas del mouse al hacer click en una figura.
# Se pasa como argumento a SelectRectangle
def __onselect(self,eclick, erelease):
if eclick.ydata > erelease.ydata:
eclick.ydata, erelease.ydata=erelease.ydata, eclick.ydata
if eclick.xdata > erelease.xdata:
eclick.xdata, erelease.xdata=erelease.xdata, eclick.xdata
plt.close()
return
# Solicita seleccionar ROIs de un área no irradiada y de otra irradiada con dosis conocida.
# Aplica un factor de corrección a la calibración
def CalibrationCorrection(self):
import ctypes # An included library with Python install.
# Pide seleccionar el recorte sin irradiar
ctypes.windll.user32.MessageBoxW(0, "Select non irradiated ROI", "", 0)
rs = self.SelectRectangle()
x = [np.int(rs.corners[0][0]), np.int(rs.corners[0][2])]
y = [np.int(rs.corners[1][0]), np.int(rs.corners[1][2])]
self.ROI = self.OD[y[0]:(y[1]+1), x[0]:(x[1]+1), 0:3]
od0 = np.array(self.ROI)
# Pide seleccionar el recorte irradiado a dosis conocida de referencia
ctypes.windll.user32.MessageBoxW(0, "Select irradiated ROI", "", 0)
rs = self.SelectRectangle()
x = [np.int(rs.corners[0][0]), np.int(rs.corners[0][2])]
y = [np.int(rs.corners[1][0]), np.int(rs.corners[1][2])]
#self.ROI = self.imagearray[y[0]:(y[1]+1),x[0]:(x[1]+1),0:3]
#self.ODref = np.mean(np.log10(65535/self.ROI),axis=(0,1))
self.ROI = self.OD[y[0]:(y[1]+1),x[0]:(x[1]+1),0:3]
odref = np.array(self.ROI)
# Solicita el valor de la dosis de referencia
dref = np.empty(3)
application_window = tk.Tk()
application_window.withdraw()
answer = simpledialog.askstring("Input", "Introduce de reference dose (Gy)",
parent=application_window)
dref.fill(np.float(answer))
application_window.destroy()
# Calcula la ODnet que corresponde a la dosis de referencia usando la curva de calibración.
self.calculate_calibration_correction_factors(od0, odref, dref)
return True
def calculate_calibration_correction_factors(self, od0, odref, dref):
# Calcula la ODnet que corresponde a la dosis de referencia usando la curva de calibración.
self.Dref = dref
self.OD0 = np.mean(od0,axis=(0,1))
self.ODref = np.mean(odref,axis=(0,1))
ODnetRefCal = np.array([0.0, 0.0, 0.0])
for c in np.array([0, 1, 2]):
ODnetRefCal[c] = self.Calibration.get_nod_from_dose(self.Dref[c], c)
# Calcula el factor de corrección de dosis
self.CalibrationCorrectionFactors = ODnetRefCal / (self.ODref - self.OD0)
# Aplica el factor de corrección. Llevamos la película a las condiciones de la calibración
self.OD = self.Calibration.OD0 + (self.OD - self.OD0) * self.CalibrationCorrectionFactors
self.OD0 = self.Calibration.OD0
def LoadFlatScanCorrFiles(self, file_path=None):
self.FlatScanCoeffList = []
count = 0
if file_path:
try:
# Open and read the JSON file
with open(file_path, 'r') as file:
coefficients = json.load(file)
self.FlatScanCoeffList = coefficients
a=1
return coefficients
except FileNotFoundError:
print("The specified file was not found.")
return None
except json.JSONDecodeError:
print("Error decoding JSON from the file.")
return None
else:
self.LoadFlatScanCorrFiles_interactive()
# Carga los ficheros con los polinomios de corrección de flatscanner y genera una lista con los coeficientes
# Se carga un fichero por cada nivel de dosis, en orden creciente con la dosis.
def _LoadFlatScanCorrFiles(self, directory=None):
self.FlatScanCoeffList = []
count = 0
if directory:
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# checking if it is a file
if os.path.isfile(f):
count = count + 1
coeff = np.array(0.001 * np.loadtxt(f, usecols=(0, 1, 2), skiprows=0))
self.FlatScanCoeffList.append(coeff)
print(f'Leidos los coeficientes de flatscanner del fichero {f.split("/")[-1]}:')
#print(coeff)
else:
self.LoadFlatScanCorrFiles_interactive()
def LoadFlatScanCorrFiles_interactive(self):
self.FlatScanCoeffList = []
count = 0
file_path = 'something'
while True:
appwindow = tk.Tk()
appwindow.withdraw()
file_path = filedialog.askopenfilename(title='Select FlatScan file {}.'.format(count + 1))
appwindow.destroy()
count = count + 1
if not file_path:
break
coeff = np.array(0.001*np.loadtxt(file_path, usecols=(0, 1, 2), skiprows=0))
self.FlatScanCoeffList.append(coeff)
def ApplyFlatScanCorr(self, rel=True):
self.FScorr = np.ones(self.imagearray.shape)
nlevels = len(self.FlatScanCoeffList) # numero de niveles de dosis
pvcenter = np.ones([len(self.FlatScanCoeffList), 3])
center = self.imagearray.shape[0]*self.pixelsize[0]*0.5 # coordenada del centro de la imagen en altura
# calcula densidad óptica en el centro
for i in range(len(self.FlatScanCoeffList)):
pvcenter[i] = [np.polyval(np.flip(self.FlatScanCoeffList[i][c]), 0.0) for c in ["R","G","B"]]
coeffarray=np.ones([len(self.FlatScanCoeffList),
len(self.FlatScanCoeffList[0]["R"]), 3])
# Pasa la lista de coeficientes a un array
color_dic = {"R":0,"G":1,"B":2}
for i in np.arange(coeffarray.shape[0]):
for k,v in self.FlatScanCoeffList[0].items():
c=color_dic[k]
for j in range(len(v)):
jj = len(v) - 1 - j
coeffarray[i, jj, c] = self.FlatScanCoeffList[i][k][j]
# Inicializa el array que va a contener los factores de corrección para cada píxel
farray = np.ones([nlevels, self.imagearray.shape[0], 3])
for h in np.arange(self.imagearray.shape[0]):
ypos = h*self.pixelsize[0] - center
for c in np.arange(3):
for nl in np.arange(nlevels):
# Factores de corrección para cada nivel de dosis
farray[nl,h,c] = np.polyval(coeffarray[nl,:,c],ypos)
for w in np.arange(self.imagearray.shape[1]):
# En cada pixel, interpola para obtener el factor de corrección correspondiente a su
# densidad optica
fcorr = self.__computeFScorrfactor(self.imagearray[h, w, c],
ypos,
farray[:, h, c],
pvcenter[:, c],rel)
self.FScorr[h, w, c] = fcorr
if rel:
# Correccion relativa
self.imagearray[h, w, c] = self.imagearray[h, w, c]/fcorr # aplica correccion relativa
else:
self.imagearray[h, w, c] = self.imagearray[h, w, c] - fcorr # aplica corrección absoluta
self.OD = np.log10(65535.0 / self.imagearray)
# Genera una imagen tif 24-bits con el mapa de corrección obtenido
FSimage = np.array((255*(self.FScorr-0.9)/0.2).astype(np.uint8))
imFS = Image.fromarray(FSimage)
imname = 'ScanCorrMap_' + self.imagefilename
imFS.save(self.workingdir+imname)
return self.FScorr
# Aplica la corrección de flatscanner.
# Sólo se aplica en la dirección transversal al movimiento de la lámpara
def _ApplyFlatScanCorr(self):
self.FScorr = np.ones(self.imagearray.shape)
nlevels = len(self.FlatScanCoeffList) # numero de niveles de dosis
odcenter = np.ones([len(self.FlatScanCoeffList), 3])
center = self.imagearray.shape[0]*self.pixelsize[0]*0.5 # coordenada del centro de la imagen en altura
# calcula densidad óptica en el centro
for i in range(len(self.FlatScanCoeffList)):
odcenter[i,:] = np.polyval(self.FlatScanCoeffList[i], center)
coeffarray=np.ones([len(self.FlatScanCoeffList),
self.FlatScanCoeffList[0].shape[0], 3])
# Pasa la lista de coeficientes a un array
for i in np.arange(coeffarray.shape[0]):
for j in np.arange(coeffarray.shape[1]):
for c in np.arange(3):
coeffarray[i, j, c] = self.FlatScanCoeffList[i][j, c]
# Inicializa el array que va a contener los factores de corrección para cada píxel
farray = np.ones([nlevels, self.imagearray.shape[0], 3])
for h in np.arange(self.imagearray.shape[0]):
ypos = h*self.pixelsize[0]
for c in np.arange(3):
for nl in np.arange(nlevels):
# Factores de corrección para cada nivel de dosis
farray[nl,h,c] = np.polyval(coeffarray[nl,:,c],ypos)
for w in np.arange(self.imagearray.shape[1]):
# En cada pixel, interpola para obtener el factor de corrección correspondiente a su
# densidad optica
fcorr = self.__computeFScorrfactor(self.OD[h, w, c],
ypos,
farray[:, h, c],
odcenter[:, c])
self.FScorr[h, w, c] = fcorr
self.OD[h, w, c] = self.OD[h, w, c]/fcorr # aplica la corrección
# Genera una imagen tif 24-bits con el mapa de corrección obtenido
FSimage = np.array((255*(self.FScorr-0.9)/0.2).astype(np.uint8))
imFS = Image.fromarray(FSimage)
imname = 'ScanCorrMap_' + self.imagefilename
imFS.save(self.workingdir+imname)
return self.FScorr
def __computeFScorrfactor(self, od, x, farr, odCentral, rel=True):
index = 0
fcorr = 1.0
nlevels = farr.shape[0]
for i in np.arange(nlevels):
odUnif = farr[i]
if od < odUnif:
index = i
if index == 0 or index == (nlevels-1):
if rel:
fcorr = farr[index]/odCentral[index]
else:
fcorr = farr[index] - odCentral[index]
if 0 < index < (nlevels - 1):
od1 = farr[index]
od2 = farr[index+1]
if rel:
f1 = od1/odCentral[index]
f2 = od2/odCentral[index+1]
else:
f1 = od1 - odCentral[index]
f2 = od2 - odCentral[index + 1]
a = (f2-f1)/(od2-od1)
b = f1 - a*od1
fcorr = a*od + b
return fcorr
# Correccion por dependencia energética
def EnergyDependence_correction(self, kint, frel):
# D_water = frel * f_cal(nod_film / kint)
# frel is apllied when generating the DoseArrays
self.kintrinsic = kint
self.fad = frel
# Selecciona el área a la que se va a aplicar la corrección
ctypes.windll.user32.MessageBoxW(0, "Select area for energy dependence correction", "", 0)
rs = self.SelectRectangle()
x = [np.int(rs.corners[0][0]), np.int(rs.corners[0][2])]
y = [np.int(rs.corners[1][0]), np.int(rs.corners[1][2])]
od_roi = self.OD[y[0]:(y[1] + 1), x[0]:(x[1] + 1), 0:3]
self.alpha = np.ones([self.OD.shape[0], self.OD.shape[1]])
lim = 0.2
cont = 0.0
cont2 = 0.0
cont_lim = od_roi.shape[0]*od_roi.shape[1]*od_roi.shape[2]
for h in np.arange(od_roi.shape[0]):
if cont2/cont_lim > 0.05:
cont2 = 0.0
for w in np.arange(od_roi.shape[1]):
for c in np.arange(3):
self.OD[h + y[0], w + x[0], c] = self.OD0[c] + \
(self.OD[h + y[0], w + x[0], c] - self.OD0[c])/self.kintrinsic
cont = cont + 1.0
cont2 = cont2 + 1.0
# Método de corrección multicanal para un parámetro multiplicativo a la densidad óptica.
# Equivalente al implementado en el programa de matlab.
def multichannel_correction1(self):
from scipy import optimize
import ctypes # An included library with Python install.
# Selecciona el área a la que se va a aplicar la corrección
ctypes.windll.user32.MessageBoxW(0, "Select area for multichannel correction", "", 0)
rs = self.SelectRectangle()
x = [np.int(rs.corners[0][0]), np.int(rs.corners[0][2])]
y = [np.int(rs.corners[1][0]), np.int(rs.corners[1][2])]
od_roi = self.OD[y[0]:(y[1] + 1), x[0]:(x[1] + 1), 0:3]
self.alpha = np.ones([self.OD.shape[0], self.OD.shape[1]])
lim = 0.2
cont = 0.0
cont2 = 0.0
cont_lim = od_roi.shape[0]*od_roi.shape[1]*od_roi.shape[2]
for h in np.arange(od_roi.shape[0]):
if cont2/cont_lim > 0.05:
cont2 = 0.0
print(f'Multichannel correction process: {np.trunc(100*cont/cont_lim)}%')
for w in np.arange(od_roi.shape[1]):
#fittet_params = optimize.minimize(self.optimization_func1,
# x0=np.array([1.0]),
# args=(od_roi[h, w, :], self.OD0[:])).x
fittet_params = self.mi_optimizacion(np.array([1.0]), od_roi[h, w, :],self.OD0[:])
if fittet_params[0] > 1.2:
fittet_params[0] = 1.2
if fittet_params[0] < 0.8:
fittet_params[0] = 0.8
self.alpha[h + y[0], w + x[0]] = fittet_params[0]
for c in np.arange(3):
od = od_roi[h, w, c]*fittet_params[0]
self.OD[h + y[0], w + x[0], c] = od
cont = cont + 1.0
cont2 = cont2 + 1.0
# Genera una imagen con el mapa de corrección multicanal obtenido
alpha_image = np.array((65535 * 0.5 + self.alpha * 1000))
np.clip(alpha_image, 0, 65535)
imname = 'MultichanelMap_' + self.imagefilename
tifffile.imwrite(self.workingdir + imname,
alpha_image.astype(np.uint16),
resolution=(self.dpi[0], self.dpi[1]))
def mi_optimizacion(self, params, od, od0):
paso = 0.002
#Determina dirección
if self.optimization_func1(params+paso, od, od0) > self.optimization_func1(params-paso, od, od0):
paso = -1*paso
nparams = params
f1 = self.optimization_func1(nparams, od, od0)
f2 = self.optimization_func1(nparams+paso, od, od0)
while f2 < f1:
f1 = f2
nparams = nparams + paso
if abs(nparams[0] - 1.0) > 0.1:
break
f2 = self.optimization_func1(nparams + paso, od, od0)
return nparams
# Función que se optimiza en el método de corrección multicanal1
def optimization_func1(self, params, od, od0):
nod = od*params[0]-od0
nod[nod < 0] = 0.0
if (1-params[0]) > 0.2:
return 10000.0
dr, dg, db = self.Calibration.get_dose_from_nod(nod)
f = 3*np.power(dr - dg, 2) + np.power(dr - db, 2) + np.power(dg - db, 2)
return f
def optimization_func2a(self, params, odnet):
nod = (odnet - params[1] * self.OD0)/(1 + params[0])
dr = self.Calibration.get_dose_from_nod(nod[0], 0)
dg = self.Calibration.get_dose_from_nod(nod[1], 1)
db = self.Calibration.get_dose_from_nod(nod[2], 2)
f = np.power(dr - dg, 2) + np.power(dr - db, 2) + np.power(params[0], 2) + np.power(params[1], 2)
return f
def optimization_func2b(self, params, odnet):
nod = (odnet - params[1] * self.OD0)/(1 + params[0])
dk = np.array([self.Calibration.get_dose_from_nod(odnet[c], c) for c in [0, 1, 2]])
d = np.array([self.Calibration.get_dose_from_nod(nod[c], c) for c in [0, 1, 2]])
delta_d = np.array([self.Calibration.Get_DerivateFromODnet(nod[c], c) for c in [0, 1, 2]])
delta_d = delta_d*(params[0]*odnet + params[1]*self.OD0)/(1 + params[0])
f = 0.0
unc = np.array([1.0, 1.0, 10.0])
for c in [0, 1, 2]:
f = f + np.power(d[c] + delta_d[c] - dk[c], 2)/unc[c]
#f = f + 0.001*np.power(params[0], 2) + 0.001*np.power(params[1], 2)
f = f + np.power(d[0]-d[1], 2) + 0.1*np.power(d[0]-d[2], 2)
return f