Alexandr Velikiy
6 years ago
committed by
rozetko
21 changed files with 221 additions and 156 deletions
@ -1,16 +1,29 @@ |
|||||||
import utils.meta |
|
||||||
from analytic_types import ModelCache |
from analytic_types import ModelCache |
||||||
|
from analytic_types.segment import Segment |
||||||
|
|
||||||
|
from typing import List, Optional |
||||||
|
|
||||||
|
|
||||||
@utils.meta.JSONClass |
|
||||||
class DetectionResult: |
class DetectionResult: |
||||||
|
|
||||||
def __init__( |
def __init__( |
||||||
self, |
self, |
||||||
cache: ModelCache = ModelCache(), |
cache: Optional[ModelCache] = None, |
||||||
segments: list = [], |
segments: Optional[List[Segment]] = None, |
||||||
last_detection_time: int = None |
last_detection_time: int = None |
||||||
): |
): |
||||||
|
if cache is None: |
||||||
|
cache = {} |
||||||
|
if segments is None: |
||||||
|
segments = [] |
||||||
self.cache = cache |
self.cache = cache |
||||||
self.segments = segments |
self.segments = segments |
||||||
self.last_detection_time = last_detection_time |
self.last_detection_time = last_detection_time |
||||||
|
|
||||||
|
# TODO: use @utils.meta.JSONClass (now it can't serialize list of objects) |
||||||
|
def to_json(self): |
||||||
|
return { |
||||||
|
'cache': self.cache, |
||||||
|
'segments': list(map(lambda segment: segment.to_json(), self.segments)), |
||||||
|
'lastDetectionTime': self.last_detection_time |
||||||
|
} |
||||||
|
@ -0,0 +1,18 @@ |
|||||||
|
from typing import Optional |
||||||
|
|
||||||
|
class Segment: |
||||||
|
''' |
||||||
|
Used for segment manipulation instead of { 'from': ..., 'to': ... } dict |
||||||
|
''' |
||||||
|
|
||||||
|
def __init__(self, from_timestamp: int, to_timestamp: int): |
||||||
|
if to_timestamp < from_timestamp: |
||||||
|
raise ValueError(f'Can`t create segment with to < from: {to_timestamp} < {from_timestamp}') |
||||||
|
self.from_timestamp = from_timestamp |
||||||
|
self.to_timestamp = to_timestamp |
||||||
|
|
||||||
|
def to_json(self): |
||||||
|
return { |
||||||
|
'from': self.from_timestamp, |
||||||
|
'to': self.to_timestamp |
||||||
|
} |
@ -1,4 +1,3 @@ |
|||||||
from utils.common import * |
from utils.common import * |
||||||
from utils.segments import * |
|
||||||
from utils.time import * |
from utils.time import * |
||||||
from utils.dataframe import * |
from utils.dataframe import * |
||||||
|
@ -1,9 +0,0 @@ |
|||||||
import pandas as pd |
|
||||||
|
|
||||||
from utils.common import timestamp_to_index |
|
||||||
|
|
||||||
def parse_segment(segment: dict, dataframe: pd.DataFrame): |
|
||||||
start = timestamp_to_index(dataframe, pd.to_datetime(segment['from'], unit='ms')) |
|
||||||
end = timestamp_to_index(dataframe, pd.to_datetime(segment['to'], unit='ms')) |
|
||||||
data = dataframe['value'][start: end + 1] |
|
||||||
return {'from': start, 'to': end, 'data': data} |
|
Loading…
Reference in new issue