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


Python Stack.add_first方法代码示例

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


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

示例1: get_z_sum

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import add_first [as 别名]
def get_z_sum(interval_length,
              wet_or_dry,
              zindex_values,
              periods_per_year,
              calibration_start_year,
              calibration_end_year,
              input_start_year):

        tempZ = Stack()
        values_to_sum = Stack()
        summed_values = Stack()

        for zindex in zindex_values:

            # we need to skip Z-index values from the list if they don't exist, this can result from empty months in the final year of the data set
            if not np.isnan(zindex):
                tempZ.push(zindex)

        # remove periods before the start of the calibration interval
        initial_calibration_period_index = (calibration_start_year - input_start_year) * periods_per_year
        for i in range(initial_calibration_period_index):

            if tempZ.size > 0:
                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
#.........这里部分代码省略.........
开发者ID:dmcevoy,项目名称:climate_indices,代码行数:103,代码来源:duration_factors.py


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