Exemplo n.º 1
0
    def execute(self, request):
        input_data = load_request(request)
        required_parameters = ["project_id", "dataset_id", "pipeline_id"]
        if set(required_parameters).issubset(input_data.keys()):
            permissions = []
            try:
                project = Project.objects.get(id=int(input_data["project_id"]))
                if not IsOwnerOfProject().has_object_permission(
                        request, self, project):
                    permissions.append("Unauthorized to access project.")
            except Project.DoesNotExist:
                project = None
            try:
                pipeline = Pipeline.objects.get(
                    id=int(input_data["pipeline_id"]))
                if not IsOwnerOfPipeline().has_object_permission(
                        request, self, pipeline):
                    permissions.append("Unauthorized to access pipeline")
            except Pipeline.DoesNotExist:
                pipeline = None
            try:
                dataset = Dataset.objects.get(id=int(input_data["dataset_id"]))
                if not IsOwnerOfDataset().has_object_permission(
                        request, self, dataset):
                    permissions.append("Unauthorized to access dataset")
            except Dataset.DoesNotExist:
                dataset = None
            if len(permissions) > 0:
                return Response(permissions,
                                status=status.HTTP_401_UNAUTHORIZED)
            if project is None or dataset is None or pipeline is None:
                message = []
                if project is None:
                    message.append("No project found for id: {}".format(
                        input_data["project_id"]))
                if dataset is None:
                    message.append("No dataset found for id: {}".format(
                        input_data["dataset_id"]))
                if pipeline is None:
                    message.append("No pipeline found for id: {}".format(
                        input_data["pipeline_id"]))
                return Response(", ".join(message),
                                status=status.HTTP_400_BAD_REQUEST)
            try:
                DaskTasks.setup_task(project_id=project.id,
                                     dataset_id=dataset.id,
                                     pipeline_id=pipeline.id)
                response = "Successfully executed pipeline"
            except Exception as ex:
                response = "Error occured attempting to execute pipeline. Message: {}".format(
                    ex)
            return Response(response, status=status.HTTP_200_OK)

        else:
            return Response(
                "Missing required parameters in POST request. Required parameters: {}"
                .format(", ".join(required_parameters)),
                status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
    def refit_model(self, request):
        input_data = load_request(request)
        required_parameters = ["project_id", "model_id", "predictive_models"]
        if set(required_parameters).issubset(input_data.keys()):
            permissions = []
            try:
                project = Project.objects.get(id=int(input_data["project_id"]))
                if not IsOwnerOfProject().has_object_permission(
                        request, self, project):
                    permissions.append("Unauthorized to access project.")
            except Project.DoesNotExist:
                project = None
            try:
                model = Model.objects.get(id=int(input_data["model_id"]))
                if not IsOwnerOfModel().has_object_permission(
                        request, self, model):
                    permissions.append("Unauthorized to access pipeline")
            except Model.DoesNotExist:
                model = None
            if len(permissions) > 0:
                return Response(permissions,
                                status=status.HTTP_401_UNAUTHORIZED)
            if model is None or project is None:
                message = []
                if project is None:
                    message.append("No project found for id: {}".format(
                        input_data["project_id"]))
                if model is None:
                    message.append("No model found for id: {}".format(
                        input_data["model_id"]))
                return Response(", ".join(message),
                                status=status.HTTP_400_BAD_REQUEST)
            p_models = {}
            for p in json.loads(input_data["predictive_models"]):
                p_models[p[0]] = int(p[1])
            m = Metadata(model, json.dumps({"predictive_models": p_models}))
            meta = m.set_metadata("ModelMetadata")

            response = {}
            DaskTasks.refit_task(project.id, model.id, p_models)
            response["project_id"] = project.id
            response["pipeline_id"] = model.pipeline.id
            response["model_id"] = model.id
            response["dataset_id"] = project.dataset
            response["model_metadata"] = meta
            response[
                "status"] = "Initiated refit for specified models for prediction"
            return Response(response, status=status.HTTP_200_OK)
        data = "Missing required parameters: {}".format(
            ", ".join(required_parameters))
        response_status = status.HTTP_200_OK
        return Response(data, status=response_status)
Exemplo n.º 3
0
 def execute(self, request):
     input_data = request.data.dict()
     required_parameters = ["workflow_id", "dataset_id", "model_id"]
     if set(required_parameters).issubset(input_data.keys()):
         try:
             workflow = Workflow.objects.get(id=int(input_data["workflow_id"]))
         except ObjectDoesNotExist:
             workflow = None
         try:
             amodel = AnalyticalModel.objects.get(id=int(input_data["model_id"]))
         except ObjectDoesNotExist:
             amodel = None
         try:
             dataset = Dataset.objects.get(id=int(input_data["dataset_id"]))
         except ObjectDoesNotExist:
             dataset = None
         if workflow is None or dataset is None or amodel is None:
             message = []
             if workflow is None:
                 message.append("No workflow found for id: {}".format(input_data["workflow_id"]))
             if dataset is None:
                 message.append("No dataset found for id: {}".format(input_data["dataset_id"]))
             if amodel is None:
                 message.append("No analytical model found for id: {}".format(input_data["model_id"]))
             return Response(", ".join(message), status=status.HTTP_400_BAD_REQUEST)
         elif IsOwnerOfLocationChild().has_object_permission(request, self, workflow):
             try:
                 DaskTasks.setup_task(dataset_id=dataset.id, amodel_id=amodel.id)
                 response = "Successfully executed analytical model"
             except Exception as ex:
                 response = "Error occured attempting to execute analytical model. Message: {}".format(ex)
             return Response(response, status=status.HTTP_200_OK)
         else:
             return Response(status=status.HTTP_401_UNAUTHORIZED)
     else:
         return Response(
             "Missing required parameters in POST request. Required parameters: {}".format(", ".join(required_parameters)),
             status.HTTP_400_BAD_REQUEST
         )
Exemplo n.º 4
0
    def evaluate(self, request):
        """

        :param request:
        :return:
        """
        input_data = load_request(request)
        required_parameters = ["project_id", "model_id"]
        if set(required_parameters).issubset(input_data.keys()):
            permissions = []
            try:
                project = Project.objects.get(id=int(input_data["project_id"]))
                if not IsOwnerOfProject().has_object_permission(
                        request, self, project):
                    permissions.append("Unauthorized to access project.")
            except Project.DoesNotExist:
                project = None
            try:
                model = Model.objects.get(id=int(input_data["model_id"]))
                if not IsOwnerOfModel().has_object_permission(
                        request, self, model):
                    permissions.append("Unauthorized to access pipeline")
            except Model.DoesNotExist:
                model = None
            if len(permissions) > 0:
                return Response(permissions,
                                status=status.HTTP_401_UNAUTHORIZED)
            if model is None or project is None:
                message = []
                if project is None:
                    message.append("No project found for id: {}".format(
                        input_data["project_id"]))
                if model is None:
                    message.append("No model found for id: {}".format(
                        input_data["model_id"]))
                return Response(", ".join(message),
                                status=status.HTTP_400_BAD_REQUEST)
            flag = input_data["flag"] if "flag" in input_data.keys() else None
            response = {}
            results = DaskTasks.evaluate(project.id, model.id, flag)
            response["project_id"] = project.id
            response["pipeline_id"] = model.pipeline.id
            response["model_id"] = model.id
            response["dataset_id"] = project.dataset
            response["results"] = results
            return Response(response, status=status.HTTP_200_OK)
        data = "Missing required parameters: {}".format(
            ", ".join(required_parameters))
        response_status = status.HTTP_200_OK
        return Response(data, status=response_status)
Exemplo n.º 5
0
 def data(self, request):
     inputs = request.data.dict()
     required_parameters = ["workflow_id", "model_id"]
     if set(required_parameters).issubset(inputs.keys()):
         try:
             workflow = Workflow.objects.get(id=int(inputs["workflow_id"]))
         except ObjectDoesNotExist:
             workflow = None
         try:
             amodel = AnalyticalModel.objects.get(id=int(inputs["model_id"]))
         except ObjectDoesNotExist:
             amodel = None
         if workflow is None or amodel is None:
             message = []
             message = message if workflow else message.append("No workflow found for id: {}".format(inputs["workflow_id"]))
             message = message if amodel else message.append("No analytical model found for id: {}".format(inputs["amodel_id"]))
             return Response(",".join(message), status=status.HTTP_400_BAD_REQUEST)
         elif IsOwnerOfLocationChild().has_object_permission(request, self, workflow):
             response = {}
             meta = Metadata(parent=amodel)
             metadata = meta.get_metadata("ModelMetadata", ['status', 'stage', 'message'])
             response["metadata"] = metadata
             completed = False
             if "stage" in metadata.keys():
                 i = metadata["stage"].split("/")
                 if int(i[0]) == int(i[1]):
                     completed = True
             if completed:
                 if amodel.model:
                     data = None
                     if "data" in inputs.keys():
                         data = pd.read_csv(StringIO(inputs["data"]))
                     response["data"] = DaskTasks.make_prediction(amodel.id, data)
                     response["dataset_id"] = amodel.dataset
             response["analytical_model_id"] = amodel.id
             response["workflow_id"] = workflow.id
             return Response(response, status=status.HTTP_200_OK)
     data = "Missing required parameters: {}".format(", ".join(required_parameters))
     response_status = status.HTTP_200_OK
     return Response(data, status=response_status)