def init_from_point(self, point_object=None, point_object_id=None): if not self.obj is None: raise BadLogic( 'Cannot call init_from_point on a used model instance') if not point_object is None: p = point_object elif not point_object_id is None: p = Point.objects_safe(pk=point_object_id).first() else: raise ValueError( 'At least one of point_object or point_object_id must be provided' ) code = p.experiment.code or list( p.experiment.data['DATASOURCES'].keys())[0][:2] self.obj = ModelCollection() self.obj.model_id = f'{code}.{self._generate_model_id()}' self.obj.status = 'READY' self.obj.model_type = ModelTypeMatcher.get_type_name(self) self.obj.point = p self.obj.experiment = p.experiment.id self.obj.step = p.step self.exp_config = ExperimentConfiguration().load_by_point_object(p) self.obj.config = self.exp_config._as_dict self.obj.created_at = datetime.now(timezone.utc) self.obj.code = code l.debug(f'model is loaded from the point {p.id}')
def _load_self_from_mongo_object(self, obj: ModelCollection, read_only: bool = True): if not self.obj is None: raise BadLogic('Cannot load model instance twice') self.read_only = read_only self.obj = obj self.exp_config = ExperimentConfiguration()._load_from_dict(obj.config) key = obj.model_file.split(':')[1] self.clf = pickle.loads(self.s3.read_binary(key)) return self
def predict_latest(self): if self.models is None: raise BadLogic( 'Model has to be loaded before being able to predict') result = None for model in self.models: Y = model.predict_latest() if result is None: result = np.full_like(Y, True) result = np.logical_and(result, Y) return result
def _load_self_from_mongo_object(self, obj: ModelCollection): if not self.obj is None: raise BadLogic('Cannot load model instance twice') self.obj = obj model_ensemble = getattr(obj, 'model_ensemble', None) if model_ensemble is None or len(obj.model_ensemble) == 0: raise MalformedModel('Model has to have "model_ensemble" field') if len(obj.model_ensemble) == 1: l.warning( f'Strange ensemble, that has only one model. Obj.id={obj.id}') self.models = [] i = 0 for model_obj in model_ensemble: self.models.append( ModelInterface.load_from_mongo_object(model_obj)) i += 1 l.debug( f'Successfully loaded ensemble model, that has {i} members. obj.id={obj.id}' )
def exp_config(self): if self.models is None: raise BadLogic('Load ensemble of models first') return self.models[0].exp_config
def latest_time(self): if self.models is None: raise BadLogic('Load ensemble of models first') return self.models[0].latest_time
def latest_time(self): if not hasattr(self, '_dataset') or self._dataset is None: raise BadLogic('to call latest_time() method attach dataset first') return self._dataset.latest_time