Browse Source

Make models sync

pull/1/head
rozetko 6 years ago
parent
commit
f79a2d9ff1
  1. 4
      analytics/detectors/pattern_detector.py
  2. 4
      analytics/models/custom_model.py
  3. 8
      analytics/models/jump_model.py
  4. 4
      analytics/models/model.py
  5. 4
      analytics/models/peaks_model.py
  6. 8
      analytics/models/step_model.py

4
analytics/detectors/pattern_detector.py

@ -62,7 +62,7 @@ class PatternDetector:
segments = self.data_prov.transform_anomalies(segments)
# TODO: pass only part of dataframe that has segments
await self.model.fit(dataframe, segments)
self.model.fit(dataframe, segments)
self.__save_model()
return 0
@ -77,7 +77,7 @@ class PatternDetector:
start_index = max(0, start_index - window_size)
dataframe = self.data_prov.get_data_range(start_index)
predicted_indexes = await self.model.predict(dataframe)
predicted_indexes = self.model.predict(dataframe)
predicted_indexes = [(x, y) for (x, y) in predicted_indexes if x >= start_index and y >= start_index]
predicted_times = self.data_prov.inverse_transform_indexes(predicted_indexes)

4
analytics/models/custom_model.py

@ -10,8 +10,8 @@ class CustomModel(Model):
# It will be saved in filesystem and loaded after server restart
self.state = {}
async def fit(self, dataframe, segments):
def fit(self, dataframe, segments):
pass
async def predict(self, dataframe):
def predict(self, dataframe):
return []

8
analytics/models/jump_model.py

@ -19,7 +19,7 @@ class JumpModel(Model):
'convolve_max': WINDOW_SIZE
}
async def fit(self, dataframe, segments):
def fit(self, dataframe, segments):
self.segments = segments
#self.alpha_finder()
data = dataframe['value']
@ -72,17 +72,17 @@ class JumpModel(Model):
else:
self.state['convolve_max'] = WINDOW_SIZE # макс метрика свертки равна отступу(WINDOW_SIZE), вау!
async def predict(self, dataframe):
def predict(self, dataframe):
data = dataframe['value']
result = await self.__predict(data)
result = self.__predict(data)
result.sort()
if len(self.segments) > 0:
result = [segment for segment in result if not utils.is_intersect(segment, self.segments)]
return result
async def __predict(self, data):
def __predict(self, data):
window_size = 24
all_max_flatten_data = data.rolling(window=window_size).mean()
all_mins = argrelextrema(np.array(all_max_flatten_data), np.less)[0]

4
analytics/models/model.py

@ -14,11 +14,11 @@ class Model(ABC):
self.segments = []
@abstractmethod
async def fit(self, dataframe: DataFrame, segments: list):
def fit(self, dataframe: DataFrame, segments: list):
pass
@abstractmethod
async def predict(self, dataframe: DataFrame) -> list:
def predict(self, dataframe: DataFrame) -> list:
pass
def save(self, model_filename: str):

4
analytics/models/peaks_model.py

@ -10,10 +10,10 @@ class PeaksModel(Model):
def __init__(self):
super()
async def fit(self, dataset, contamination=0.005):
def fit(self, dataset, contamination=0.005):
pass
async def predict(self, dataframe):
def predict(self, dataframe):
array = dataframe['value'].as_matrix()
window_size = 20
# window = np.ones(101)

8
analytics/models/step_model.py

@ -19,7 +19,7 @@ class StepModel(Model):
'convolve_max': 570000
}
async def fit(self, dataframe, segments):
def fit(self, dataframe, segments):
self.segments = segments
data = dataframe['value']
confidences = []
@ -47,17 +47,17 @@ class StepModel(Model):
else:
self.state['convolve_max'] = 570000
async def predict(self, dataframe):
def predict(self, dataframe):
data = dataframe['value']
result = await self.__predict(data)
result = self.__predict(data)
result.sort()
if len(self.segments) > 0:
result = [segment for segment in result if not utils.is_intersect(segment, self.segments)]
return result
async def __predict(self, data):
def __predict(self, data):
window_size = 24
all_max_flatten_data = data.rolling(window=window_size).mean()
all_mins = argrelextrema(np.array(all_max_flatten_data), np.less)[0]

Loading…
Cancel
Save