Exemplo n.º 1
0
    def delete(self, id):

        f_dao = FileDao(self.db_session())
        f = f_dao.retrieve(id=id)
        f_dao.delete(f)

        return self.output_html('<h3>Not implemented</h3', 200)
Exemplo n.º 2
0
    def post(self, id):

        parser = reqparse.RequestParser()
        parser.add_argument('file',
                            type=FileStorage,
                            required=True,
                            location='files')
        args = parser.parse_args()
        repository_dao = RepositoryDao(self.db_session())
        repository = repository_dao.retrieve(id=id)
        args['repository'] = repository
        args['storage_id'] = generate_string()
        args['storage_path'] = os.path.join(current_app.root_path,
                                            self.config()['UPLOAD_DIR'],
                                            args['storage_id'])
        args['file'].save(args['storage_path'])
        args['name'] = args['file'].filename
        args['extension'] = '.'.join(args['name'].split('.')[1:])
        args['content_type'] = 'application/octet-stream'
        args['media_link'] = args['storage_path']
        args['size'] = 0
        # Remove 'file' element in the arguments because the File constructor
        # does not expect it
        del args['file']
        f_dao = FileDao(self.db_session())
        f = f_dao.create(**args)

        return f.to_dict(), 201
Exemplo n.º 3
0
    def post(self):

        parser = reqparse.RequestParser()
        parser.add_argument('file',
                            type=FileStorage,
                            required=True,
                            location='files')
        args = parser.parse_args()

        args['storage_id'] = generate_string()
        args['storage_path'] = os.path.join(current_app.root_path,
                                            self.config()['UPLOAD_DIR'],
                                            args['storage_id'])
        args['file'].save(args['storage_path'])
        args['name'] = args['file'].filename
        args['extension'] = '.'.join(args['name'].split('.')[1:])
        args['content_type'] = 'application/octet-stream'
        args['media_link'] = args['storage_path']
        args['size'] = 0

        del args['file']

        f_dao = FileDao(self.db_session())
        f = f_dao.create(**args)

        html = '<h3>Thanks for uploading your file!</h3>'
        html += '<p>You can get it here: <a href="/files/{}/content">download</a></p>'.format(
            f.id)
        html += '<br>'
        html += '<p>Next step is to create a new classifier by clicking the button below.</p>'
        html += '<form method="post" action="/classifiers">'
        html += '  <input type="submit" value="Create classifier">'
        html += '</form>'

        return self.output_html(html, 201)
Exemplo n.º 4
0
    def get(self, id):

        f_dao = FileDao(self.db_session())
        f = f_dao.retrieve(id=id)

        return send_from_directory(os.path.join(current_app.root_path,
                                                self.config()['UPLOAD_DIR']),
                                   filename=f.storage_id), 200
Exemplo n.º 5
0
    def get(self, id, file_id):

        self.check_permission('retrieve:repository@{}'.format(id))
        self.check_permission('retrieve:file@{}'.format(file_id))
        repository_dao = RepositoryDao(self.db_session())
        repository = repository_dao.retrieve(id=id)
        f_dao = FileDao(self.db_session())
        f = f_dao.retrieve(id=file_id)
        if f.repository != repository:
            raise FileNotInRepositoryException(f.name, repository.name)

        return f.to_dict(), 200
Exemplo n.º 6
0
    def get(self, id, file_id):

        self.check_permission('retrieve:repository@{}'.format(id))
        self.check_permission('retrieve:file@{}'.format(file_id))
        repository_dao = RepositoryDao(self.db_session())
        repository = repository_dao.retrieve(id=id)
        f_dao = FileDao(self.db_session())
        f = f_dao.retrieve(id=file_id)
        if f.repository != repository:
            raise FileNotInRepositoryException(f.name, repository.name)

        return send_from_directory(os.path.join(current_app.root_path,
                                                self.config()['UPLOAD_DIR']),
                                   filename=f.storage_id), 200
Exemplo n.º 7
0
    def delete(self, id, file_set_id, file_id):

        self.check_permission('retrieve:repository@{}'.format(id))
        self.check_permission('retrieve,update:file-set@{}'.format(file_set_id))
        repository_dao = RepositoryDao(self.db_session())
        repository = repository_dao.retrieve(id=id)
        file_set_dao = FileSetDao(self.db_session())
        file_set = file_set_dao.retrieve(id=file_set_id)
        if file_set.repository != repository:
            raise FileSetNotInRepositoryException(file_set.name, repository.name)
        f_dao = FileDao(self.db_session())
        f = f_dao.retrieve(id=file_id)
        if f.repository != repository:
            raise FileNotInRepositoryException(f.name, repository.name)
        file_set.remove_file(f)
        file_set_dao.save(file_set)

        return file_set.to_dict(), 200
Exemplo n.º 8
0
    def post(self, id):

        parser = reqparse.RequestParser()
        parser.add_argument('file',
                            type=FileStorage,
                            required=True,
                            location='files')
        args = parser.parse_args()

        args['storage_id'] = generate_string()
        args['storage_path'] = os.path.join(current_app.root_path,
                                            self.config()['UPLOAD_DIR'],
                                            args['storage_id'])
        args['file'].save(args['storage_path'])
        args['name'] = args['file'].filename
        args['extension'] = '.'.join(args['name'].split('.')[1:])
        args['content_type'] = 'application/octet-stream'
        args['media_link'] = args['storage_path']
        args['size'] = 0
        del args['file']

        f_dao = FileDao(self.db_session())
        f = f_dao.create(**args)
        session_dao = SessionDao(self.db_session())
        session = session_dao.retrieve(id=id)

        print('Running prediction...')

        # Load classifier object file and run the prediction

        # Run the prediction.

        # Delete CSV file

        html = '<h3>Congratulations!</h3>'
        html += '<p>You have successfully completed your prediction!</p>'
        html += '<p>Here are the results:</p>'

        return self.output_html(html, 201)
Exemplo n.º 9
0
    def post(self, id):

        parser = reqparse.RequestParser()
        parser.add_argument('file',
                            type=FileStorage,
                            required=True,
                            location='files')
        parser.add_argument('R', type=str, location='form')
        parser.add_argument('target_column', type=str, location='form')
        parser.add_argument('exclude_columns', type=str, location='form')
        parser.add_argument('nr_iters', type=str, location='form')
        parser.add_argument('nr_folds', type=str, location='form')
        args = parser.parse_args()

        R = args['R']
        target_column = args['target_column']
        exclude_columns = args['exclude_columns'].split(',')
        nr_iters = int(args['nr_iters'])
        nr_folds = int(args['nr_folds'])

        args['storage_id'] = generate_string()
        args['storage_path'] = os.path.join(current_app.root_path,
                                            self.config()['UPLOAD_DIR'],
                                            args['storage_id'])
        args['file'].save(args['storage_path'])
        args['name'] = args['file'].filename
        args['extension'] = '.'.join(args['name'].split('.')[1:])
        args['content_type'] = 'application/octet-stream'
        args['media_link'] = args['storage_path']
        args['size'] = 0

        del args['file']
        del args['R']
        del args['target_column']
        del args['exclude_columns']
        del args['nr_iters']
        del args['nr_folds']

        f_dao = FileDao(self.db_session())
        f = f_dao.create(**args)

        classifier_dao = ClassifierDao(self.db_session())
        classifier = classifier_dao.retrieve(id=id)
        print('Training classifier {} on file {}'.format(
            classifier.name, f.name))
        if R == 'true':
            print('R scripting is not implemented yet...')

        # After classifier training finishes, create a session that captures the results
        print('Calculating classifier performance...')
        scores = 0
        features = pd.read_csv(f.storage_path)
        x, y = get_xy(features,
                      target_column=target_column,
                      exclude_columns=exclude_columns)
        for i in range(nr_iters):
            for train, test in StratifiedKFold(n_splits=nr_folds).split(x, y):
                _, score = score_svm(x, y, train, test)
                scores += score
        avg_score = scores / (nr_iters * nr_folds)

        path = f.storage_path + '.classifier'
        print('Building optimized classifier and storing in {}'.format(path))
        classifier_model = train_svm(x, y)
        joblib.dump(classifier_model, path)

        session_dao = SessionDao(self.db_session())
        session = session_dao.create(
            **{
                'classifier': classifier,
                'training_file_path': f.storage_path,
                'classifier_file_path': path,
            })

        html = '<h3>Congratulations!</h3>'

        html += '<p>You have successfully trained your classifier! It has an average '
        html += 'classification accuracy of {} after {} iterations and {} folds.</p>'.format(
            avg_score, nr_iters, nr_folds)

        html += '<p>The next step is to use your classifier for predictions. Again, upload a<br>'
        html += 'CSV file with the cases you want to predict.</p>'
        html += '<form method="post" enctype="multipart/form-data" action="/sessions/{}/predictions">'.format(
            session.id)
        html += '  <input type="file" name="file">'
        html += '  <input type="submit" value="Upload predictions">'
        html += '</form>'

        return self.output_html(html, 201)
Exemplo n.º 10
0
    def post(self, id):

        f_dao = FileDao(self.db_session())
        f = f_dao.retrieve(id=id)

        return f.to_dict(), 201
Exemplo n.º 11
0
    def post(self, id):
        """
        Creates a new training session for classifier {id}. The classifier uses the
        uploaded file for training.
        :param id: 
        :return: 
        """
        parser = reqparse.RequestParser()
        parser.add_argument('file', type=FileStorage, required=True, location='files')
        parser.add_argument('R', type=str, location='form')
        parser.add_argument('target_column', type=str, location='form')
        parser.add_argument('exclude_columns', type=str, location='form')
        parser.add_argument('nr_iters', type=str, location='form')
        parser.add_argument('nr_folds', type=str, location='form')
        parser.add_argument('subject_id', type=str, required=True, location='form')
        args = parser.parse_args()

        subject_id = args['subject_id']
        R = args['R']
        target_column = args['target_column']
        exclude_columns = args['exclude_columns'].split(',')
        nr_iters = int(args['nr_iters'])
        nr_folds = int(args['nr_folds'])

        args['storage_id'] = generate_string()
        args['storage_path'] = os.path.join(current_app.root_path, self.config()['UPLOAD_DIR'], args['storage_id'])
        args['file'].save(args['storage_path'])
        args['name'] = args['file'].filename
        args['extension'] = '.'.join(args['name'].split('.')[1:])
        args['content_type'] = 'application/octet-stream'
        args['media_link'] = args['storage_path']
        args['size'] = 0

        del args['subject_id']
        del args['file']
        del args['R']
        del args['target_column']
        del args['exclude_columns']
        del args['nr_iters']
        del args['nr_folds']

        f_dao = FileDao(self.db_session())
        f = f_dao.create(**args)

        classifier_dao = ClassifierDao(self.db_session())
        classifier = classifier_dao.retrieve(id=id)
        print('Training classifier {} on file {}'.format(classifier.name, f.name))
        if R == 'true':
            os.system('Rscript ./R/svm.R 1')

        # After classifier training finishes, create a session that captures the results
        print('Calculating classifier performance...')
        features = pd.read_csv(f.storage_path, index_col=subject_id)
        x, y = get_xy(features, target_column=target_column)

        scores = 0
        # Run performance evaluation on classifier
        for i in range(nr_iters):
            for train, test in StratifiedKFold(n_splits=nr_folds).split(x, y):
                _, score = score_svm(x, y, train, test)
                scores += score
        avg_score = scores / (nr_iters * nr_folds)

        path = f.storage_path + '.classifier'
        print('Building optimized classifier and storing in {}'.format(path))
        classifier_model = train_svm(x, y)
        joblib.dump(classifier_model, path)

        session_dao = SessionDao(self.db_session())
        session = session_dao.create(**{
            'classifier': classifier,
            'training_file_path': f.storage_path,
            'classifier_file_path': path,
        })

        html = '<h3>Step 3 - View training session</h3>'
        html += '<p>You have successfully trained your classifier. Click the link below<br>'
        html += 'To view the training session and upload a file with cases to predict.</p>'

        html += '<a href="/sessions/{}">Session {}</a>'.format(session.id, session.id)

        return self.output_html(html, 201)