# -*- coding: utf-8 -*-
"""
homogeneity report elements to include inside
templates/homogeneities/homogeneity.html
"""

from flask import Blueprint, render_template
from flask_login import login_required

from pathlib import Path

# from metrologist.upload_image.forms import UploadImage
from metrologist.field_illum.models import Homogeneity

# from metrologist.metroloj import common, homo
# if using metroloj pip package
from metroloj import common, homo

from tempfile import TemporaryDirectory

blueprint = Blueprint(
    "field_illum", __name__, url_prefix="/field_illum", static_folder="../static"
)

PARENT1 = Path(__file__).resolve().parents[1]
PARENT2 = Path(__file__).resolve().parents[2]
@blueprint.route("/")

def field_illum():
    """
    this function generates the homogeneity report elements on the input image in
    order to include them on the corresponding web page.
    the generated plots will be saved in a temporary dir.
    """

    # load input image
    image = common.get_images_from_multi_tiff(
        path=str(PARENT1) + "/field_illum/examples/homogeneity_ex1.tif",
        )
 
    # temp path for plots

    # graph_path_temp = TemporaryDirectory()
    # intensity_plot_path = graph_path_temp.name + "/" + "intensity_plot.png"
    # norm_intensity_profile_path = graph_path_temp.name + "/" + "norm_intensity_profile.png"

    graph_path_temp = str(PARENT2) +  "/assets/img/"
    intensity_plot_path = graph_path_temp + "intensity_plot.png"
    norm_intensity_profile_path = graph_path_temp + "norm_intensity_profile.png"

    build_path = "build/img/"
    intensity_plot_build_path = build_path + "intensity_plot.png"
    norm_intensity_profile_build_path = build_path + "norm_intensity_profile.png"

    # get homogeneity report element for input image
    # plots will be saved into graph_path_temp (arg: save_path)
 
    max_region_info  = homo.get_max_intensity_region_table(image)

    profile_stat_table = homo.get_profile_statistics_table(image)

    _ , intensity_plot_data = homo.get_intensity_plot(
        image,
        save_path=intensity_plot_path
        )

    norm_intensity_matrix = homo.get_norm_intensity_matrix(image)
    homo.get_norm_intensity_profile(
        image,
        save_path=norm_intensity_profile_path
        )

    # define Homogeneity object attributes 

    homogeneity_record = Homogeneity(
        max_region_table_nb_pixels = max_region_info["nb pixels"],
        max_region_table_center_of_mass = max_region_info["center of mass"],
        max_region_table_max_intensity = max_region_info["max intensity"],
        profile_stat_table_location = profile_stat_table["location"],
        profile_stat_table_intensity = profile_stat_table["intensity"],
        profile_stat_table_intensity_relative_to_max = profile_stat_table["intensity relative to max"],
        intensity_plot_data_x_axis_V_seg = intensity_plot_data["x_axis_V_seg"],
        intensity_plot_data_y_axis_V_seg = intensity_plot_data["y_axis_V_seg"],
        intensity_plot_data_x_axis_H_seg = intensity_plot_data["x_axis_H_seg"],
        intensity_plot_data_y_axis_H_seg = intensity_plot_data["y_axis_H_seg"],
        intensity_plot_data_x_axis_diagUD = intensity_plot_data["x_axis_diagUD"],
        intensity_plot_data_y_axis_diagUD = intensity_plot_data["y_axis_diagUD"],
        intensity_plot_data_x_axis_diagDU = intensity_plot_data["x_axis_diagDU"],
        intensity_plot_data_y_axis_diagDU = intensity_plot_data["y_axis_diagDU"],
        norm_intensity_data = norm_intensity_matrix
        )

    return render_template(
        "field_illum/field_illum.html",
        homogeneity_record=homogeneity_record,
        intensity_plot_build_path=intensity_plot_build_path,
        norm_intensity_profile_build_path=norm_intensity_profile_build_path
        )

# -*- coding: utf-8 -*-
"""
homogeneity report elements to include inside
templates/homogeneities/homogeneity.html
"""

from flask import Blueprint, render_template
from flask_login import login_required

from pathlib import Path

# from metrologist.upload_image.forms import UploadImage
from metrologist.field_illum.models import Homogeneity

# from metrologist.metroloj import common, homo
# if using metroloj pip package
from metroloj import common, homo

from tempfile import TemporaryDirectory

blueprint = Blueprint(
    "field_illum", __name__, url_prefix="/field_illum", static_folder="../static"
)

PARENT1 = Path(__file__).resolve().parents[1]
PARENT2 = Path(__file__).resolve().parents[2]
@blueprint.route("/")

def field_illum():
    """
    this function generates the homogeneity report elements on the input image in
    order to include them on the corresponding web page.
    the generated plots will be saved in a temporary dir.
    """

    # load input image
    image = common.get_images_from_multi_tiff(
        path=str(PARENT1) + "/field_illum/examples/homogeneity_ex1.tif",
        )
 
    # temp path for plots

    # graph_path_temp = TemporaryDirectory()
    # intensity_plot_path = graph_path_temp.name + "/" + "intensity_plot.png"
    # norm_intensity_profile_path = graph_path_temp.name + "/" + "norm_intensity_profile.png"

    graph_path_temp = str(PARENT2) +  "/assets/img/"
    intensity_plot_path = graph_path_temp + "intensity_plot.png"
    norm_intensity_profile_path = graph_path_temp + "norm_intensity_profile.png"

    build_path = "build/img/"
    intensity_plot_build_path = build_path + "intensity_plot.png"
    norm_intensity_profile_build_path = build_path + "norm_intensity_profile.png"

    # get homogeneity report element for input image
    # plots will be saved into graph_path_temp (arg: save_path)
 
    max_region_info  = homo.get_max_intensity_region_table(image)

    profile_stat_table = homo.get_profile_statistics_table(image)

    _ , intensity_plot_data = homo.get_intensity_plot(
        image,
        save_path=intensity_plot_path
        )

    norm_intensity_matrix = homo.get_norm_intensity_matrix(image)
    homo.get_norm_intensity_profile(
        image,
        save_path=norm_intensity_profile_path
        )

    # define Homogeneity object attributes 

    homogeneity_record = Homogeneity(
        max_region_table_nb_pixels = max_region_info["nb pixels"],
        max_region_table_center_of_mass = max_region_info["center of mass"],
        max_region_table_max_intensity = max_region_info["max intensity"],
        profile_stat_table_location = profile_stat_table["location"],
        profile_stat_table_intensity = profile_stat_table["intensity"],
        profile_stat_table_intensity_relative_to_max = profile_stat_table["intensity relative to max"],
        intensity_plot_data_x_axis_V_seg = intensity_plot_data["x_axis_V_seg"],
        intensity_plot_data_y_axis_V_seg = intensity_plot_data["y_axis_V_seg"],
        intensity_plot_data_x_axis_H_seg = intensity_plot_data["x_axis_H_seg"],
        intensity_plot_data_y_axis_H_seg = intensity_plot_data["y_axis_H_seg"],
        intensity_plot_data_x_axis_diagUD = intensity_plot_data["x_axis_diagUD"],
        intensity_plot_data_y_axis_diagUD = intensity_plot_data["y_axis_diagUD"],
        intensity_plot_data_x_axis_diagDU = intensity_plot_data["x_axis_diagDU"],
        intensity_plot_data_y_axis_diagDU = intensity_plot_data["y_axis_diagDU"],
        norm_intensity_data = norm_intensity_matrix
        )

    return render_template(
        "field_illum/field_illum.html",
        homogeneity_record=homogeneity_record,
        intensity_plot_build_path=intensity_plot_build_path,
        norm_intensity_profile_build_path=norm_intensity_profile_build_path
        )

# -*- coding: utf-8 -*- homogeneity report elements to include inside templates/homogeneities/homogeneity.html # from metrologist.upload_image.forms import UploadImage # from metrologist.metroloj import common, homo # if using metroloj pip package this function generates the homogeneity report elements on the input image in     order to include them on the corresponding web page.     the generated plots will be saved in a temporary dir. # load input image # temp path for plots # graph_path_temp = TemporaryDirectory() # intensity_plot_path = graph_path_temp.name + "/" + "intensity_plot.png" # norm_intensity_profile_path = graph_path_temp.name + "/" + "norm_intensity_profile.png" # get homogeneity report element for input image # plots will be saved into graph_path_temp (arg: save_path) # define Homogeneity object attributes