|
|
@ -4,7 +4,7 @@ import logging |
|
|
|
import pandas as pd |
|
|
|
import pandas as pd |
|
|
|
from typing import Optional |
|
|
|
from typing import Optional |
|
|
|
from models import AnalyticUnitCache |
|
|
|
from models import AnalyticUnitCache |
|
|
|
from concurrent.futures import Executor |
|
|
|
from concurrent.futures import Executor, CancelledError |
|
|
|
import asyncio |
|
|
|
import asyncio |
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger('AnalyticUnitWorker') |
|
|
|
logger = logging.getLogger('AnalyticUnitWorker') |
|
|
@ -14,16 +14,26 @@ class AnalyticUnitWorker: |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, analytic_unit_id: str, detector: detectors.Detector, executor: Executor): |
|
|
|
def __init__(self, analytic_unit_id: str, detector: detectors.Detector, executor: Executor): |
|
|
|
self.analytic_unit_id = analytic_unit_id |
|
|
|
self.analytic_unit_id = analytic_unit_id |
|
|
|
self.detector = detector |
|
|
|
self._detector = detector |
|
|
|
self.executor: Executor = executor |
|
|
|
self._executor: Executor = executor |
|
|
|
|
|
|
|
self._training_feature: asyncio.Future = None |
|
|
|
|
|
|
|
|
|
|
|
async def do_learn( |
|
|
|
async def do_train( |
|
|
|
self, segments: list, data: pd.DataFrame, cache: Optional[AnalyticUnitCache] |
|
|
|
self, segments: list, data: pd.DataFrame, cache: Optional[AnalyticUnitCache] |
|
|
|
) -> AnalyticUnitCache: |
|
|
|
) -> AnalyticUnitCache: |
|
|
|
new_cache: AnalyticUnitCache = await asyncio.get_event_loop().run_in_executor( |
|
|
|
self._training_feature = asyncio.get_event_loop().run_in_executor( |
|
|
|
self.executor, self.detector.train, data, segments, cache |
|
|
|
self._executor, self._detector.train, data, segments, cache |
|
|
|
) |
|
|
|
) |
|
|
|
return new_cache |
|
|
|
try: |
|
|
|
|
|
|
|
new_cache: AnalyticUnitCache = await self._training_feature |
|
|
|
|
|
|
|
return new_cache |
|
|
|
|
|
|
|
except CancelledError as e: |
|
|
|
|
|
|
|
return cache |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def do_predict(self, data: pd.DataFrame, cache: Optional[AnalyticUnitCache]) -> dict: |
|
|
|
async def do_predict(self, data: pd.DataFrame, cache: Optional[AnalyticUnitCache]) -> dict: |
|
|
|
return self.detector.predict(data, cache) |
|
|
|
return self._detector.predict(data, cache) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cancel(self): |
|
|
|
|
|
|
|
if self._training_feature is not None: |
|
|
|
|
|
|
|
self._training_feature.cancel() |
|
|
|