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) segments = self.data_prov.transform_anomalies(segments)
# TODO: pass only part of dataframe that has segments # TODO: pass only part of dataframe that has segments
await self.model.fit(dataframe, segments) self.model.fit(dataframe, segments)
self.__save_model() self.__save_model()
return 0 return 0
@ -77,7 +77,7 @@ class PatternDetector:
start_index = max(0, start_index - window_size) start_index = max(0, start_index - window_size)
dataframe = self.data_prov.get_data_range(start_index) 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_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) 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 # It will be saved in filesystem and loaded after server restart
self.state = {} self.state = {}
async def fit(self, dataframe, segments): def fit(self, dataframe, segments):
pass pass
async def predict(self, dataframe): def predict(self, dataframe):
return [] return []

8
analytics/models/jump_model.py

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

4
analytics/models/peaks_model.py

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

8
analytics/models/step_model.py

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

Loading…
Cancel
Save