Пример #1
0
def test_duration():
    assert Duration.set_time('2:5:0').to_str() == '2:5:0'
    assert Duration.set_time('2:5:0').to_duration() == 7500
    assert Duration.set_duration(7500).to_duration() == 7500
    assert Duration.set_duration(7500).to_str() == '2:5:0'
    assert (Duration.set_duration(7500) - duration_delta(m=5)).to_str() == '2:0:0'
    assert (Duration.set_duration(7500) - duration_delta(s=5)).to_str() == '2:4:55'
Пример #2
0
    def _trans_set2result(self, vod_set: dict) -> typing.Dict:
        result = {}
        for station_num, vod_range in vod_set.items():
            result[station_num] = []
            for start, s_range in vod_range.items():
                s = start.split('_')[-1]
                for min_r, max_r in s_range:
                    min_r_str = (Duration.set_time(s) + duration_delta(
                        s=min_r *
                        config.THUMBNAIL_SIZE.DURATION_SEC)).to_str()
                    max_r_str = (Duration.set_time(s) + duration_delta(
                        s=max_r *
                        config.THUMBNAIL_SIZE.DURATION_SEC)).to_str()
                    result[station_num].append((min_r_str, max_r_str))

        return result
Пример #3
0
def sub_path(path: str, num, label: int = 0) -> str:
    p = Path(path)
    station_str, raw_duration = p.stem.split('_')
    duration = (Duration.set_time(raw_duration) +
                duration_delta(s=num)).to_str()
    file_name = f'{station_str}_{duration}_{label}{p.suffix}'
    file_dir = 'img'
    sub_img_path = Path(file_dir, file_name)
    return str(sub_img_path)
Пример #4
0
    def _find_top(self, raw_data: typing.List,
                  time_duration: int) -> typing.Optional[typing.List]:
        if not raw_data or not time_duration:
            return

        cnt = pd.DataFrame(raw_data, columns=['index', 'value'])

        perfect_duration = (
            Duration.set_duration(time_duration) -
            duration_delta(m=self.perfect_start_min)).to_duration()
        per_index = time_duration // len(cnt)
        diff_duration = time_duration - perfect_duration
        perfect_start = diff_duration // per_index

        y = pd.DataFrame(raw_data[perfect_start:], columns=['index', 'value'])
        sma_period = perfect_start * self.smooth_factor
        Y = ta.SMA(y['value'].values.astype('float64'),
                   timeperiod=sma_period).tolist()

        top = []
        for i, d in enumerate(Y):
            if d > 0 and i < len(Y) - 1:
                if (Y[i - 1] <= d and d >= Y[i + 1]) or (i == 0
                                                         and d >= Y[i + 1]):
                    top.append((i + perfect_start, d))

        def row_sma(row):
            cond = (row['start_index'] <= cnt['index']) & (cnt['index'] <=
                                                           row['index'])
            max_id = cnt.where(cond).dropna()['value'].idxmax()
            result = cnt.loc[max_id]
            row['ori_index'] = result['index']
            row['ori_value'] = result['value']
            ori_duration = result['index'] * per_index
            start_duration = Duration.set_duration(ori_duration - MIN_SEC *
                                                   self.range_factor).to_str()
            end_duration = Duration.set_duration(ori_duration + MIN_SEC *
                                                 self.range_factor).to_str()
            row['ori_range_duration'] = (start_duration, end_duration)
            return row

        top_df = pd.DataFrame(top, columns=['index', 'value'])
        top_df['value'].where(top_df['value'] > top_df['value'].mean(),
                              inplace=True)
        top_df = top_df.where(top_df['value'] > 0).dropna()
        top_df['start_index'] = top_df['index'] - sma_period + 1
        top_df['index'] = top_df['index']
        target = top_df.apply(row_sma, axis=1)

        return target['ori_range_duration'].drop_duplicates().to_list()
Пример #5
0
 def sub_path(
     self,
     path,
     file_dir: str,
     num,
     label: int = 0,
 ) -> str:
     p = Path(path)
     station_str, raw_duration = p.stem.split('_')
     duration = (Duration.set_time(raw_duration) +
                 duration_delta(s=num)).to_str()
     file_name = f'{duration}_{label}{p.suffix}'
     # self.log.info(f'[{self.user_id}:{station_str}] {file_name}')
     file_dir = file_dir
     # sub_img_path = Path(file_dir, station_str, file_name)
     sub_img_path = Path(file_dir, f'{station_str}_{file_name}')
     if not sub_img_path.parent.exists():
         sub_img_path.parent.mkdir(parents=True)
     return str(sub_img_path)
Пример #6
0
def pl(raw_data, time_duration, station_num):
    if raw_data and time_duration:
        cnt = pd.DataFrame(raw_data, columns=['index', 'value'])
        x = list(range(0, len(cnt)))

        perfect_duration = (Duration.set_duration(time_duration) -
                            duration_delta(m=5)).to_duration()
        per_index = time_duration // len(cnt)
        diff_duration = time_duration - perfect_duration
        perfect_start = diff_duration // per_index

        y = pd.DataFrame(raw_data[perfect_start:], columns=['index', 'value'])

        sma_period = perfect_start * 2
        Y = ta.SMA(y['value'].values.astype('float64'),
                   timeperiod=sma_period).tolist()

        top = []

        for i, d in enumerate(Y):
            if d > 0 and i < len(Y) - 1:
                if (Y[i - 1] <= d and d >= Y[i + 1]) or (i == 0
                                                         and d >= Y[i + 1]):
                    top.append((i + perfect_start, d))
                # elif Y[i - 1] >= d and d <= Y[i + 1]:
                #     bottom.append((i, d))

        def row_sma(row):
            cond = (row['start_index'] <= cnt['index']) & (cnt['index'] <=
                                                           row['index'])
            max_id = cnt.where(cond).dropna()['value'].idxmax()
            result = cnt.loc[max_id]
            row['ori_index'] = result['index']
            row['ori_value'] = result['value']
            row['ori_duration'] = result['index'] * per_index
            row['ori_range_duration'] = (row['ori_duration'] - MIN_SEC * 3,
                                         row['ori_duration'] + MIN_SEC * 3)
            return row

        top_df = pd.DataFrame(top, columns=['index', 'value'])
        top_df['value'].where(top_df['value'] > top_df['value'].mean(),
                              inplace=True)
        top_df = top_df.where(top_df['value'] > 0).dropna()
        top_df['start_index'] = top_df['index'] - sma_period + 1
        top_df['index'] = top_df['index']

        a = top_df.apply(row_sma, axis=1)

        plt.plot(x, cnt['value'].values, 'r', linewidth=1, label='ori')
        plt.plot(x[5:], Y, 'b', linewidth=1, label=f'sma-{sma_period}')

        plt.scatter(top_df['index'],
                    top_df['value'],
                    100,
                    marker='^',
                    label='max')
        plt.scatter(a['ori_index'],
                    a['ori_value'],
                    100,
                    marker='v',
                    label='ori_max')
        plt.title(station_num)
        plt.legend()
        plt.show()