Пример #1
0
 def get(self, project_id):
     user_id = get_jwt_identity()
     result = ProjectModel.find_one_by_id(project_id, user_id)
     columns_filter_id = result.columns_filter_id
     columns_filter = ProjectUnstructureModel.find_by_object_id(
         ObjectId(columns_filter_id))
     return {
         'message': SUCCESS_MESSAGE.format('columns_filter'),
         'columns_filter': columns_filter['columns']
     }
Пример #2
0
    def post(self, experiment_name):
        user_id = get_jwt_identity()
        data = Prediction.parser.parse_args()
        raw_data = data['data']
        dataset = File.read_json(raw_data)

        result = ExperimentModel.find_by_name(user_id, experiment_name)
        y_col = result.y_col
        datasource_id = result.datasource_id
        project_id = result.project_id

        result = DatasourceModel.find_one_by_id(user_id, datasource_id)
        user_schema_name = result.user_schema_name
        user_table_name = result.user_table_name
        columns = [
            col['column_name'] for col in DatasourceModel.get_columns(
                user_schema_name, user_table_name)
        ]

        try:
            columns_filter_id = ProjectModel.find_one_by_id(
                project_id, user_id).columns_filter_id
        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_GET_COLUMNS_FILTER_ID.format(project_id)
            }, 500

        try:
            columns_filter = ProjectUnstructureModel.find_by_object_id(
                ObjectId(columns_filter_id))['columns']
        except Exception as e:
            return {
                'message': ERROR_CANNOT_GET_COLUMNS_FILTER.format(project_id)
            }, 500
        try:
            predict = Predicting(experiment_name, dataset, columns,
                                 columns_filter, y_col, user_id)
            predict.decorate_dataframe()
            predict.load_model()
            predict.predict()
            dataset = predict.merge_predict_to_dataframe()
            json_data = Dataset(dataset).get_json()
        except Exception as e:
            print(e)
            return {
                'message':
                ERROR_CANNOT_GET_PREDICT_WITH_DATA.format(experiment_name)
            }, 500
        # try:
        #     DatasourceModel.append_datasource(dataset, user_schema_name, user_table_name)
        # except Exception as e:
        #     return {'message': ERROR_CANNOT_INSERT_DB}, 500

        return {'data': json_data}, 200
Пример #3
0
 def delete(self, project_id):
     user_id = get_jwt_identity()
     try:
         project = ProjectModel.find_one_by_id(project_id, user_id)
         project.delete_from_db()
         return {'message': SUCCESS_MESSAGE.format(project_id)}, 200
     except Exception as e:
         return {
             'message':
             'This project has many experiments, please remove experiment first.'
         }, 500
Пример #4
0
 def get(self, project_id):
     user_id = get_jwt_identity()
     data = ProjectModel.find_one_by_id(project_id, user_id)
     collection = CollectionModel.find_one_by_id(data.collection_id,
                                                 user_id)
     datasource = DatasourceModel.find_one_by_id(user_id,
                                                 data.datasource_id)
     return {
         'project_id': data.project_id,
         'project_name': data.project_name,
         'project_description': data.project_description,
         'datasource': {
             'datasource_id': datasource.datasource_id,
             'datasource_name': datasource.datasource_name,
             'datasource_description': datasource.datasource_description
         },
         'collection': {
             'collection_id': collection.collection_id,
             'collection_name': collection.collection_name,
             'collection_description': collection.collection_description,
         }
     }
Пример #5
0
    def post(self, project_id, experiment_name):
        user_id = get_jwt_identity()
        data = Experiment.parser.parse_args()
        experiment_description = data['experiment_description']
        datasource_id = data['datasoruce_id']
        model_id = data['model_id']
        model_parameter = data['model_parameter']
        y_col = data['y_col']

        # --START--Checking retrived data
        experiment = ExperimentModel(experiment_name, experiment_description,
                                     datasource_id, model_id, y_col,
                                     project_id, user_id)
        if experiment.find_by_name(user_id, experiment_name):
            return {
                'message': ERROR_DUPLICATE_NAME.format(experiment_name)
            }, 500

        try:
            result = DatasourceModel.find_one_by_id(user_id, datasource_id)
            datasource_data = {
                'datasource_name': result.datasource_name,
                'datasource_description': result.datasource_description,
                'user_schema_name': result.user_schema_name,
                'user_table_name': result.user_table_name,
                'user_id': str(result.created_at)
            }

        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_FIND_DATASOURCE_ID.format(datasource_id)
            }, 500

        try:
            result = ModelModel.find_first_by_id(model_id)
            model_name = result.model_name

        except Exception as e:
            return {
                'message': ERROR_CANNOT_FIND_MODEL_ID.format(model_id)
            }, 500

        # --END--Checking retrived data

        try:
            dataframe = DatasourceModel(**datasource_data).get_datasource()
        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_GET_DATASET.format(
                    datasource_data['user_table_name'])
            }, 500

        try:
            columns_filter_id = ProjectModel.find_one_by_id(
                project_id, user_id).columns_filter_id
        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_GET_COLUMNS_FILTER_ID.format(project_id)
            }, 500

        try:
            columns_filter = ProjectUnstructureModel.find_by_object_id(
                ObjectId(columns_filter_id))['columns']
        except Exception as e:
            return {
                'message': ERROR_CANNOT_GET_COLUMNS_FILTER.format(project_id)
            }, 500

        try:
            training = Training(experiment_name, dataframe, model_name,
                                model_parameter, columns_filter, y_col,
                                user_id)
            training.train_test()
            training.create_and_fit_model()
            training.save_model()
            training.load_model()
            y_pred = training.predict()
            cal_score = training.cal_score()
        except Exception as e:
            print(e)
            return {'message': ERROR_CANNOT_TRAIN_MODEL_WITH_DATA}, 500

        try:

            mongo = ExperimentUnstructureModel(cal_score, model_parameter)
            cal_score_id = str(mongo.save_cal_score_to_db())
            model_parameter_id = str(mongo.save_model_parameter_to_db())
        except Exception as e:
            return {
                'message': ERROR_CANNOT_INSRET_MONGO.format(experiment_name)
            }, 500

        try:
            experiment.save_to_db()
        except Exception as e:
            return {'message': ERROR_CANNOT_INSERT_DB}, 500

        try:
            result = ExperimentModel.find_by_name(user_id, experiment_name)
            if result:
                result.save_cal_score_to_db(cal_score_id)
                result.save_model_parameter_to_db(model_parameter_id)
        except Exception as e:
            return {'message': ERROR_CANNOT_INSRET_SCORE.format(e)}, 500

        return {
            'message': '{} have already trained'.format(experiment_name)
        }, 200
Пример #6
0
    def put(self, experiment_id):
        user_id = get_jwt_identity()
        experiment = ExperimentModel.find_by_id(experiment_id)
        try:
            result = DatasourceModel.find_one_by_id(user_id,
                                                    experiment.datasource_id)
            datasource_data = {
                'datasource_name': result.datasource_name,
                'datasource_description': result.datasource_description,
                'user_schema_name': result.user_schema_name,
                'user_table_name': result.user_table_name,
                'user_id': str(result.created_at)
            }

        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_FIND_DATASOURCE_ID.format(
                    experiment.datasource_id)
            }, 500

        try:
            result = ModelModel.find_first_by_id(experiment.model_id)
            model_name = result.model_name

        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_FIND_MODEL_ID.format(experiment.model_id)
            }, 500

        try:
            dataframe = DatasourceModel(**datasource_data).get_datasource()
        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_GET_DATASET.format(
                    datasource_data['user_table_name'])
            }, 500

        try:
            columns_filter_id = ProjectModel.find_one_by_id(
                experiment.project_id, user_id).columns_filter_id
        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_GET_COLUMNS_FILTER_ID.format(
                    experiment.project_id)
            }, 500

        try:
            columns_filter = ProjectUnstructureModel.find_by_object_id(
                ObjectId(columns_filter_id))['columns']
        except Exception as e:
            return {
                'message': ERROR_CANNOT_GET_COLUMNS_FILTER.format(project_id)
            }, 500

        try:
            model_parameter = ExperimentUnstructureModel.find_model_parameter_by_object_id(
                ObjectId(experiment.model_parameter_id))['model_parameter']
        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_GET_MODEL_PARAMETER.format(
                    experiment.model_parameter_id)
            }, 500

        training = Training(experiment.experiment_name, dataframe, model_name,
                            model_parameter, columns_filter, experiment.y_col)
        training.train_test()
        training.create_and_fit_model()
        training.save_model()
        training.load_model()
        y_pred = training.predict()
        cal_score = training.cal_score()

        try:
            mongo = ExperimentUnstructureModel(cal_score, model_parameter)
            cal_score_id = str(mongo.save_cal_score_to_db())
        except Exception as e:
            return {
                'message':
                ERROR_CANNOT_INSRET_MONGO.format(experiment.experiment_name)
            }, 500

        try:
            experiment.cal_score_id = cal_score_id
            experiment.save_to_db()
        except Exception as e:
            return {'message': ERROR_CANNOT_INSERT_DB}, 500

        return {'message': SUCCESS_MESSAGE.format(experiment_id)}, 200