def create(model_id: int, payload: dict) -> int: """ Validate and add predictions from a model to the database :params model_id, payload :raises DataError :returns ID of the prediction """ version = payload['version'] try: semver.VersionInfo.parse(version) except Exception as e: raise "Version Must be SemVer" prediction_dto = PredictionDTO() prediction_dto.model_id = model_id prediction_dto.version = payload['version'] prediction_dto.tile_zoom = payload['tileZoom'] prediction_dto.inf_list = payload['infList'] prediction_dto.inf_type = payload['infType'] prediction_dto.inf_binary = payload['infBinary'] prediction_dto.inf_supertile = payload['infSupertile'] prediction_dto.validate() new_prediction = Prediction() try: new_prediction.create(prediction_dto) except sqlalchemy.exc.IntegrityError as e: if isinstance(e.orig, UniqueViolation): raise VersionExists else: raise e return new_prediction.id
def get(model_id: int, bbox: list, latest=False): """ Fetch latest predictions from a model for the given bbox :params model_id, bbox :raises PredictionsNotFound :returns predictions """ if (latest): # get the latest version latest_version = MLModelVersion.get_latest_version(model_id) if (latest_version is None): raise PredictionsNotFound('Predictions not found') else: version_id = latest_version.id predictions = Prediction.get_latest_predictions_in_bbox( model_id, version_id, bbox) else: predictions = Prediction.get_all_predictions_in_bbox( model_id, bbox) if (len(predictions) == 0): raise PredictionsNotFound('Predictions not found') data = [] for prediction in predictions: prediction_dto = Prediction.as_dto(prediction) data.append(prediction_dto.to_primitive()) return data
def get_prediction_by_id(prediction_id: int): """ Get a prediction by ID :params prediction_id :returns prediction """ prediction = Prediction.get(prediction_id) if prediction: return Prediction.as_dto(prediction) else: raise PredictionsNotFound
def get_all_by_model(model_id: int): """ Fetch all predictions of the given model :params model_id :returns predictions :raises PredictionsNotFound """ predictions = Prediction.get_predictions_by_model(model_id) prediction_dtos = [] for prediction in predictions: prediction_dtos.append(Prediction.as_dto(prediction).to_primitive()) return prediction_dtos
def export(prediction_id: int): prediction = Prediction.get(prediction_id) if (prediction): stream = prediction.export() return stream else: raise NotFound('Prediction does not exist')
def create(model_id: int, version_id: int, payload: dict) -> int: """ Validate and add predictions from a model to the database :params model_id, version_id, payload :raises DataError :returns ID of the prediction """ prediction_dto = PredictionDTO() prediction_dto.model_id = model_id prediction_dto.version_id = version_id prediction_dto.bbox = payload['bbox'] prediction_dto.tile_zoom = payload['tileZoom'] prediction_dto.validate() new_prediction = Prediction() new_prediction.create(prediction_dto) return new_prediction.id
def tilejson(model_id, prediction_id): """ Get the TileJSON of the prediction id given :params model_id :params prediction_id :returns dict """ tiles = PredictionTile.count(prediction_id) if tiles.count == 0: raise PredictionsNotFound('No Prediction Tiles exist') ml_model = MLModel.get(model_id) prediction = Prediction.get(prediction_id) tilejson = { "tilejson": "2.1.0", "name": ml_model.name, "description": ml_model.project_url, "inferences": PredictionTile.inferences(prediction_id), "token": CONFIG.EnvironmentConfig.MAPBOX_TOKEN, "attribution": ml_model.source, "version": prediction.version, "scheme": "xyz", "type": "vector", "tiles": [ "/v1/model/{0}/prediction/{1}/tiles/{{z}}/{{x}}/{{y}}.mvt". format(model_id, prediction_id) ], "minzoom": 0, "maxzoom": prediction.tile_zoom, "bounds": PredictionTile.bbox(prediction_id) } return tilejson
def patch(prediction_id: int, update: dict) -> int: """ Patch a prediction by ID :params prediction_id :params update :returns prediction """ prediction = Prediction.get(prediction_id) if (prediction): prediction.link(update) return prediction_id else: raise NotFound('Prediction does not exist')