############ Figure 3  - Source code 1 
import cv2
from skimage import morphology
import matplotlib.colors as colors
import pandas as pd
import seaborn as sns
from scipy import stats


grid = plt.GridSpec(6, 10, wspace=0.2, hspace=0.7)
plt.figure(figsize = (8, 4))



ax1 = plt.subplot(grid[0:3, 0:3])

img_col = cv2.imread(r"MAX_C1-Hh2X_12_col.tif", cv2.IMREAD_ANYDEPTH)

mygreens = colors.LinearSegmentedColormap.from_list('mygreens', ['#000000', '#00FF00'])
myreds = colors.LinearSegmentedColormap.from_list('mygreens', ['#000000', '#FF0000'])

footprint = morphology.disk(3)
res = morphology.white_tophat(img_col, footprint)
img_col = img_col - res
plt.imshow(img_col[450:850, 180:800], cmap = mygreens, interpolation='nearest', aspect='auto', origin='upper')
plt.plot([210,370], [190,190], "w", linewidth = 2)
plt.plot([210,370], [120,120], "w", linewidth = 2)
plt.plot([210, 210], [120, 190], "w", linewidth = 2)
plt.plot([370, 370], [120, 190], "w", linewidth = 2)
plt.axis("off")
plt.title("hh(+/+)")





ax2 = plt.subplot(grid[0:3, 3:6])
img_col = cv2.imread(r"MAX_C1-Hh1X_14_col.tif", cv2.IMREAD_ANYDEPTH)
footprint = morphology.disk(3)
res = morphology.white_tophat(img_col, footprint)
img_col = img_col - res
plt.imshow(img_col[420:820, 170:790], cmap = mygreens, interpolation='nearest', aspect='auto', origin='upper') #
plt.plot([220,380], [190,190], "w", linewidth = 2)
plt.plot([220,380], [120,120], "w", linewidth = 2)
plt.plot([220, 220], [120, 190], "w", linewidth = 2)
plt.plot([380, 380], [120, 190], "w", linewidth = 2)
plt.axis("off")
plt.title("hh(+/-)")






ax3 = plt.subplot(grid[3:6, 0:3])

img_dpp = cv2.imread(r"MAX_C2-Hh2X_12_dpp.tif", cv2.IMREAD_ANYDEPTH)
footprint = morphology.disk(3)
res = morphology.white_tophat(img_dpp, footprint)
img_dpp = img_dpp - res
plt.imshow(img_dpp[450:850, 180:800], cmap = myreds, interpolation='nearest', aspect='auto', origin='upper') 
plt.plot([210,370], [190,190], "w", linewidth = 2)
plt.plot([210,370], [120,120], "w", linewidth = 2)
plt.plot([210, 210], [120, 190], "w", linewidth = 2)
plt.plot([370, 370], [120, 190], "w", linewidth = 2)
plt.axis("off")




ax4 = plt.subplot(grid[3:6, 3:6])
img_dpp = cv2.imread(r"MAX_C2-Hh1X_14_dpp.tif", cv2.IMREAD_ANYDEPTH)
footprint = morphology.disk(3)
res = morphology.white_tophat(img_dpp, footprint)
img_dpp = img_dpp - res
plt.imshow(img_dpp[420:820, 170:790], cmap = myreds, interpolation='nearest', aspect='auto', origin='upper') #
plt.plot([220,380], [190,190], "w", linewidth = 2)
plt.plot([220,380], [120,120], "w", linewidth = 2)
plt.plot([220, 220], [120, 190], "w", linewidth = 2)
plt.plot([380, 380], [120, 190], "w", linewidth = 2)
plt.axis("off")



ax5 = plt.subplot(grid[1:5, 7:10])

anchuras = pd.read_csv("anchuras_variosT.csv")
anchuras02 = anchuras[anchuras["T"] == 0.2]
diferencias_medianas = pd.read_csv("diferencias_medianas_variosT.csv")
diferencias_medianas02 = diferencias_medianas[diferencias_medianas["T"] == 0.2]

median_col2x = anchuras02[anchuras02["case"] == "col(hh+/+)"]["width"].median()
median_dpp2x = anchuras02[anchuras02["case"] == "dpp(hh+/+)"]["width"].median()
median_col1x = anchuras02[anchuras02["case"] == "col(hh+/-)"]["width"].median()
median_dpp1x = anchuras02[anchuras02["case"] == "dpp(hh+/-)"]["width"].median()

dif_median_col = diferencias_medianas02[diferencias_medianas02["target"] == "col"]["median diferences"].iloc[0]
dif_median_dpp = diferencias_medianas02[diferencias_medianas02["target"] == "dpp"]["median diferences"].iloc[0]

print("diference_median_col = ", dif_median_col)
print("diference_median_dpp = ", dif_median_dpp)

p_col = diferencias_medianas02[diferencias_medianas02["target"] == "col"]["p_value"]
p_dpp = diferencias_medianas02[diferencias_medianas02["target"] == "dpp"]["p_value"]

print("p_value_col = ", p_col)
print("p_value_dpp = ", p_dpp)

my_pal = {"col(hh+/+)":"green", "dpp(hh+/+)":"red", "col(hh+/-)":"green", "dpp(hh+/-)":"red", "col":"green", "dpp":"red"}
sns.boxplot(data = anchuras02, x = "background", y = "width", hue = "target" , palette = my_pal, showfliers = False)
plt.xticks([0,1.1], ["hh(+/+)", "hh(+/-)"])
x_linea = 1.5
plt.plot([x_linea, x_linea], [median_dpp1x, median_dpp2x], "k")
plt.plot([x_linea, x_linea], [median_col1x, median_col2x], "k")
plt.plot([x_linea-0.03, x_linea],[median_dpp2x, median_dpp2x], "k")
plt.plot([x_linea-0.03, x_linea],[median_dpp1x, median_dpp1x], "k")
plt.plot([x_linea-0.03, x_linea],[median_col2x, median_col2x], "k")
plt.plot([x_linea-0.03, x_linea],[median_col1x, median_col1x], "k")
plt.text(x_linea + 0.03, (median_dpp1x + median_dpp2x)/2, str(round(dif_median_dpp,2)))
plt.text(x_linea + 0.03, (median_col1x + median_col2x)/2, str(round(dif_median_col,2)))
plt.text(x_linea-0.05, 35, r"$\Delta x$")


plt.plot([0.2, 1.2], [38.5, 38.5], "k")
plt.text(0.6, 39, "**")
plt.plot([-0.2, 0.8], [22.7, 22.7], "k")
plt.text(0.1, 23.2, "***")

plt.ylim(0,42)
plt.xlim(-0.5, 2.1)
plt.legend().set_visible(False)
plt.xlabel("")
plt.ylabel("width ($\mu m$)")


plt.text(-7.2, 55, "a", fontsize = 14, weight = "bold")
plt.text(-6.8, 26, "Col", fontsize = 14, weight = "bold",  color = "lime")
plt.text(-4.2, 55, "b", fontsize = 14, weight = "bold")
plt.text(-7.2, 19.5, "c", fontsize = 14, weight = "bold")
plt.text(-6.8, -10, r"$\beta$Gal", fontsize = 14, weight = "bold",  color = "red")
plt.text(-4.2, 19.5, "d", fontsize = 14, weight = "bold")
plt.text(-1, 45, "e", fontsize = 14, weight = "bold")

#plt.savefig("figure3.png", dpi = 512, bbox_inches = "tight")
plt.show()