scientific_comp_projects/CODE/[python]thesis_old_scripts/ph_calibration.py
2021-10-29 15:16:40 +02:00

121 lines
4.0 KiB
Python

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 6 09:58:07 2018
@author: Armando
"""
import cv2
import glob
import ReadIM
from skimage import exposure
import numpy as np
from scipy.optimize import curve_fit
from functions_utile import draw_rectangle
import matplotlib.pyplot as plt
#Variable initialization
pH_images ={}
courbe = []
rectangle_final = []
pH_max_value_array = []
file_name = []
images = {}
#Obtain the ph images to make the courbe (these images have already been treated by DaVis - spacially )
working_directory = "C:\\Users\\Armando\\Desktop\\python_processing\\ph\\zero-one"
file_name = glob.glob(working_directory + "\\*.im7")
for e in range(len(file_name)):
if file_name[e].find('pH') != -1:
vbuff, vatts = ReadIM.extra.get_Buffer_andAttributeList(file_name[e])
v_array, vbuff = ReadIM.extra.buffer_as_array(vbuff)
img_rescaled = exposure.rescale_intensity(v_array)
name = file_name[e].rsplit('\\', 1)[1]
images[name] = img_rescaled
mean = str(np.nanmean(images[name]))
print (name + ' ' + mean)
del(v_array,name,file_name,img_rescaled, working_directory)
#%%
#Make a dictonarie (pH_images) with pH value as key and the array associated as value.
#The max pH value is also obtained
images_list_form = list(images.items())
pH_max_value = 0
for e in range(len(images_list_form)):
if images_list_form[e][0].find('pH') != -1:
pH = images_list_form[e][0].replace('.im7','').rsplit('pH', 1)[1]
for y in range(len(images_list_form[e][1][0])-1):
for x in range(len(images_list_form[ e][1][0][0])-1):
if np.isinf(images_list_form[e][1][0][y][x]):
images_list_form[e][1][0][y][x] = 0.0001
elif np.isnan(images_list_form[e][1][0][y][x]):
images_list_form[e][1][0][y][x] = 0.0001
elif images_list_form[e][1][0][y][x] ==0:
images_list_form[e][1][0][y][x] = 0.001
pH_images[pH] = images_list_form[e][1]
#cv2.namedWindow("Image" + str(e),cv2.WINDOW_NORMAL) #Create the window to show image
#cv2.imshow("Image" + str(e), pH_images[pH])
pH_value = float(pH)
if pH_value > pH_max_value:
pH_max_value = pH_value
pH_max_value_array = images_list_form[e][1][0]
rectangle_final = [[10,10],[y-10,x-10]]
#del(images_list_form,e,images,x,y,pH_value,pH)
#%%
pH_images_list = list(pH_images.items())
courbe = []
for e in range(len(pH_images_list)):
normalized_photo = np.divide(pH_max_value_array,pH_images_list[e][1][0])
courbe.append([float(pH_images_list[e][0]),np.nanmean(normalized_photo, dtype = np.float64)])
del(e)
#%%
courbe.sort()
courbe = np.array(courbe, dtype=np.float64)
x1 = courbe[:,0]
y1 = courbe[:,1]
def tanh_fit(x, a, b, c, d):
return a*(np.tanh(b*x+c)) + d
def inverse_tanhfit(y ,a ,b ,c ,d):
return (1/2*np.log((y+a-d)/(-y+a+d))-c)/(b)
popt, pcov = curve_fit(tanh_fit, x1, y1,bounds=([0,0,-15,0], [1., 2.,5,1.]))
fig = plt.figure()
plt.title('Normalized intensity in function of pH')
plt.xlabel('pH', color='plum')
plt.ylabel('Normalized intensity (I/I\u2080)', color='0.5') # grayscale color
plt.grid(b=True, which='major', color='k', linestyle='-')
plt.grid(b=True, which='minor', color='k', linestyle='--')
plt.plot(x1,y1, 'r.')
x_full = np.arange(-1,15,0.01)
y_full = np.arange(0,1,0.01)
plt.plot(x_full, tanh_fit(x_full, *popt), '--',color = 'black',label='fit: A=%.3f, B=%.3f, C=%.3f D=%.3f \n A*tanh(B*pH+C) + D' % tuple(popt))
#plt.plot(y_full, inverse_tanhfit(y_full, *popt), '--',color = 'blue')
plt.legend()
plt.show()
x_min = 0
x_max = courbe[len(courbe)-1][0]
y_min = tanh_fit(x_min, *popt)
y_max = tanh_fit(x_max, *popt)
del(x1,y1,x_full)