# Analyze signal data in Thermal image

import os
import cv2
import numpy as np
import pandas as pd
from plantcv.plantcv import params
from plantcv.plantcv import outputs
from plotnine import ggplot, aes, geom_line, labs
from plantcv.plantcv.threshold import binary as binary_threshold
from plantcv.plantcv.visualize import histogram


def analyze_thermal_values(thermal_array, mask, histplot=False, label="default"):
    """This extracts the thermal values of each pixel writes the values out to
       a file. It can also print out a histogram plot of pixel intensity
       and a pseudocolor image of the plant.

    Inputs:
    array        = numpy array of thermal values
    mask         = Binary mask made from selected contours
    histplot     = if True plots histogram of intensity values
    label        = optional label parameter, modifies the variable name of observations recorded

    Returns:
    analysis_image = output image

    :param thermal_array: numpy.ndarray
    :param mask: numpy.ndarray
    :param histplot: bool
    :param label: str
    :return analysis_image: ggplot
    """

    # Store debug mode
    debug = params.debug

    # apply plant shaped mask to image and calculate statistics based on the masked image
    masked_thermal = thermal_array[np.where(mask > 0)]
    maxtemp = np.amax(masked_thermal)
    mintemp = np.amin(masked_thermal)
    avgtemp = np.average(masked_thermal)
    mediantemp = np.median(masked_thermal)

    # call the histogram function
    params.debug = None
    hist_fig, hist_data = histogram(thermal_array, mask=mask)
    bin_labels, hist_percent = hist_data['pixel intensity'].tolist(), hist_data['proportion of pixels (%)'].tolist()

    # Store data into outputs class
    outputs.add_observation(sample=label, variable='max_temp', trait='maximum temperature',
                            method='plantcv.plantcv.analyze_thermal_values', scale='degrees', datatype=float,
                            value=maxtemp, label='degrees')
    outputs.add_observation(sample=label, variable='min_temp', trait='minimum temperature',
                            method='plantcv.plantcv.analyze_thermal_values', scale='degrees', datatype=float,
                            value=mintemp, label='degrees')
    outputs.add_observation(sample=label, variable='mean_temp', trait='mean temperature',
                            method='plantcv.plantcv.analyze_thermal_values', scale='degrees', datatype=float,
                            value=avgtemp, label='degrees')
    outputs.add_observation(sample=label, variable='median_temp', trait='median temperature',
                            method='plantcv.plantcv.analyze_thermal_values', scale='degrees', datatype=float,
                            value=mediantemp, label='degrees')
    outputs.add_observation(sample=label, variable='thermal_frequencies', trait='thermal frequencies',
                            method='plantcv.plantcv.analyze_thermal_values', scale='frequency', datatype=list,
                            value=hist_percent, label=bin_labels)
    params.debug = debug
    analysis_image = None
    if histplot is True:
        params.device += 1

        # change column names of "hist_data"
        hist_fig = hist_fig + labs(x="Temperature C", y="Proportion of pixels (%)")
        if params.debug == "print":
            hist_fig.save(os.path.join(params.debug_outdir, str(params.device) + '_therm_histogram.png'),
                          verbose=False)
        elif params.debug == "plot":
            print(hist_fig)
        analysis_image = hist_fig
    # Store images
    outputs.images.append(analysis_image)

    return analysis_image




@@ -327,16 +327,106 @@
 stogram%0A
+from plantcv.plantcv import deprecation_warning%0Afrom plantcv.plantcv._debug import _debug%0A
 %0A%0Adef an
@@ -471,24 +471,8 @@
 sk, 
-histplot=False, 
 labe
@@ -482,16 +482,31 @@
 default%22
+, histplot=None
 ):%0A    %22
@@ -1190,16 +1190,184 @@
    %22%22%22%0A%0A
+    if histplot is not None:%0A        deprecation_warning(%22'histplot' will be deprecated in the future version of plantCV. This function plots histogram by default. %22)%0A%0A
     # St
@@ -3347,63 +3347,8 @@
 bug%0A
-    analysis_image = None%0A    if histplot is True:%0A    
     
@@ -3367,20 +3367,16 @@
 e += 1%0A%0A
-    
     # ch
@@ -3408,20 +3408,16 @@
 t_data%22%0A
-    
     hist
@@ -3492,70 +3492,46 @@
 )%22)%0A
-   
+%0A
     
- if params.debug == %22print%22:%0A            hist_fig.save(
+_debug(visual=hist_fig, filename=
 os.p
@@ -3581,17 +3581,17 @@
 vice) + 
-'
+%22
 _therm_h
@@ -3606,122 +3606,13 @@
 .png
-'),%0A                          verbose=False)%0A        elif params.debug == %22plot%22:%0A            print(hist_fig)%0A    
+%22))%0A%0A
     

Update analyze_thermal_values.py