当前位置: 首页>>代码示例>>Python>>正文


Python Stack.sorted_list方法代码示例

本文整理汇总了Python中stack.Stack.sorted_list方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.sorted_list方法的具体用法?Python Stack.sorted_list怎么用?Python Stack.sorted_list使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stack.Stack的用法示例。


在下文中一共展示了Stack.sorted_list方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_z_sum

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import sorted_list [as 别名]

#.........这里部分代码省略.........
                tempZ.pop()
            else:
                break

        calibration_periods_left = (calibration_end_year - calibration_start_year + 1) * periods_per_year;

        # get the first interval length of values from the end of the calibration period working backwards, creating the first sum_value of interval periods
        sum_value = 0
        for i in range(interval_length):
            if tempZ.isEmpty():
                i = interval_length
            else:
                # pull a value off the end of the list
                z = tempZ.pop()

                # reduce the remaining number of calibration interval periods we have left to process
                calibration_periods_left -= 1

                '''
                /* assumes that nCalibrationPeriods is >= length, reasonable
                 ** This is a reasonable assumption and does not hurt if
                 ** anything if false--just calibrate over a slightly longer
                 ** interval, which is already way too short in that case */
                '''
                if (not np.isnan(z) and (MISSING_VALUE != z)):

                    # add to the sum_value
                    sum_value += z

                    # add to the array of values we've used for the initial sum_value
                    values_to_sum.add_first(z)

                else:
                    # reduce the loop counter so we don't skip a calibration interval period
                    i -= 1

        # if we're dealing with wet conditions then we want to be using positive numbers, and if dry conditions
        # then we need to be using negative numbers, so we introduce a sign variable to help with this
        if wet_or_dry == 'wet':
            sign = 1
        else:  # dry
            sign =-1

        # now for each remaining Z value, recalculate the sum_value based on last value in the list to sum_value and the next Z value
        max_sum = sum_value
        summed_values.add_first(sum_value)
        while (tempZ.size > 0) and (calibration_periods_left > 0):

            # take the next Z-index value off the end of the list
            z = tempZ.pop()

            # reduce by one period for each removal
            calibration_periods_left -= 1

            if not np.isnan(z) and (MISSING_VALUE != z) and not values_to_sum.isEmpty():

                # come up with a new sum_value for this new group of values to sum_value

                # remove the last value from both the sum_value and the values to sum_value array
                sum_value -= values_to_sum.peek()
                values_to_sum.pop()

                # add to the sum_value
                sum_value += z

                # update the values to sum_value and summed values lists
                values_to_sum.add_first(z);
                summed_values.add_first(sum_value);

            # update the maximum sum_value value if we have a new max
            if (sign * sum_value) > (sign * max_sum):
                max_sum = sum_value

        # highest reasonable is the highest (or lowest) value that is not due to some freak anomaly in the data.
        # "freak anomaly" is defined as a value that is either
        #   1) 25% higher than the 98th percentile
        #   2) 25% lower than the 2nd percentile
        if wet_or_dry == 'wet':
            safe_percentile_index = int(summed_values.size * 0.98)
        else:  # DRY
            safe_percentile_index = int(summed_values.size * 0.02)

        # sort the list of sums into ascending order and get the sum_value value referenced by the safe percentile index
        summed_values_list = summed_values.sorted_list()
        sum_at_safe_percentile = summed_values_list[safe_percentile_index]

        # find the highest reasonable value out of the summed values
        highest_reasonable_value = 0.0
        reasonable_tolerance_ratio = 1.25
        while (summed_values.size > 0):
            sum_value = summed_values.pop()
            if (sign * sum_value > 0):
                if ((sum_value / sum_at_safe_percentile) < reasonable_tolerance_ratio):
                    if (sign * sum_value > sign * highest_reasonable_value):
                        highest_reasonable_value = sum_value

        if wet_or_dry == 'wet':
            return highest_reasonable_value
        else:  # DRY
            return max_sum
开发者ID:dmcevoy,项目名称:climate_indices,代码行数:104,代码来源:duration_factors.py


注:本文中的stack.Stack.sorted_list方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。