def identify_homogeneous_region(region_sums, region_stds, region_means, region_boundaries, debug=False,
                                peak_threshold=PEAK_THRESHOLD):
    """

    :param region_sums:
    :param region_boundaries:
    :param peak_threshold:
    :param debug:
    :return: detected boundaries [start_row, end_row, start_col, end_col]
    """

    # # calculate first and second derivatives
    diff1 = numpy.diff(region_stds)
    #
    # rate_of_change = []
    # rate_of_change2 = []
    #
    # for i in range(0, len(region_sums)-4):
    #     rate_of_change.append((region_sums[i+4] - region_sums[i])/4)
    #
    # for i in range(0, len(rate_of_change)-4):
    #     rate_of_change2.append((rate_of_change[i+4] - rate_of_change[i])/4)
    #
    # diff2 = numpy.diff(diff1)
    #
    # peaks = peakutils.indexes(rate_of_change2, thres=peak_threshold)
    #
    # if len(peaks) > 0:
    #     # print peaks
    #     # print [diff2[peak_position] for peak_position in peaks]
    #     peak_values = [rate_of_change2[peak_position] for peak_position in peaks]
    #
    #     # print peak_values
    #     argmax = numpy.argmax(peak_values)
    #     boundaries = region_boundaries[peaks[argmax]]
    # else:
    #     boundaries = region_boundaries[-1]
    #

    if len(diff1) > 3:
        max_index = numpy.argmax(diff1[3:]) + 2
        boundaries = region_boundaries[max_index]

        if debug:
            plt.figure()
            # plt.plot(peaks, [rate_of_change2[p] for p in peaks], 'o')
            plt.plot(region_stds)
            plt.plot(diff1, label="diff1")
            plt.plot(max_index, diff1[max_index], 'x')
            # plt.plot(diff2, label="diff2")
            # plt.plot(rate_of_change)
            # plt.plot(rate_of_change2)
            plt.show()

    else:
        boundaries = region_boundaries[-1]



    segment = [boundaries[0], boundaries[1]]

    return boundaries, segment
def identify_homogeneous_region ( region_sums , region_stds , region_means , region_boundaries , debug = False , peak_threshold = PEAK_THRESHOLD ) : diff1 = numpy . diff ( region_stds ) if len ( diff1 ) > 3 : max_index = numpy . argmax ( diff1 [ 3 : ] ) + 2 boundaries = region_boundaries [ max_index ] if debug : plt . figure ( ) plt . plot ( region_stds ) plt . plot ( diff1 , label = "diff1" ) plt . plot ( max_index , diff1 [ max_index ] , 'x' ) plt . show ( ) else : boundaries = region_boundaries [ - 1 ] segment = [ boundaries [ 0 ] , boundaries [ 1 ] ] return boundaries , segment
:param region_sums:
    :param region_boundaries:
    :param peak_threshold:
    :param debug:
    :return: detected boundaries [start_row, end_row, start_col, end_col]