Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Predict app #121

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Prev Previous commit
Next Next commit
Refactor for travis ci imports and docstrings
Patrick Kuehn committed Oct 16, 2019
commit 74ed97753cd99458b4a3279c7092338922d59844
2 changes: 1 addition & 1 deletion autofocus/predict/app/app.py
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ def classify_zip():

file = ZipArchive(request.files["file"], app.config["UPLOAD_FOLDER"])
if not file.hasImages():
validator.error['file'] = "No image files detected in the zip file."
validator.error["file"] = "No image files detected in the zip file."
validator.abort()

# Extract files
1 change: 1 addition & 0 deletions autofocus/predict/app/models/File.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

from werkzeug import secure_filename


3 changes: 1 addition & 2 deletions autofocus/predict/app/models/Predictor.py
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ def predict_multiple(self, files):

Parameters:
files: Dict with File objects of image file

Returns:
dict: Dictionary of probabilities for each file in files
"""
@@ -45,7 +45,6 @@ def predict_multiple(self, files):
predictions[key] = self.getProbabilities()
return predictions


def getProbabilities(self):
"""
Return formated Probabilities
9 changes: 5 additions & 4 deletions autofocus/predict/app/models/ZipArchive.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from zipfile import ZipFile

from .File import File
from ..requests.Validator import ALLOWED_IMAGE_FILES
from ..utils import allowed_file
@@ -46,7 +47,7 @@ def listAllImages(self, extensions=ALLOWED_IMAGE_FILES):

Parameters:
extensions: Array of allowed image extensions

Returns:
array: Array of filenames matching the extension
"""
@@ -58,7 +59,7 @@ def hasImages(self, extensions=ALLOWED_IMAGE_FILES):

Parameters:
extensions: Array of allowed image extensions

Returns:
boolean: True if zip has images
"""
@@ -74,9 +75,9 @@ def extractAll(self, path=None, members=None):
Parameters:
path: Path to store files
members: Files to extract

Returns:
array: Array of extracted File objects
array: Array of extracted File objects
"""
self.zip.extractall(path, members)
extractedFiles = {}
12 changes: 6 additions & 6 deletions autofocus/predict/app/requests/PredictRequestValidator.py
Original file line number Diff line number Diff line change
@@ -3,9 +3,7 @@


class PredictRequestValidator(Validator):
"""
Validate request for endpoint predict
"""
"""Validate request for endpoint predict"""

def validate(self):
"""
@@ -18,11 +16,13 @@ def validate(self):
"""
self.error = {}

file = self.request.files.get('file', None)
file = self.request.files.get("file", None)
if not file:
self.error['file'] = "No file given."
self.error["file"] = "No file given."
elif not allowed_file(file.filename, ALLOWED_IMAGE_FILES):
self.error['file'] = "File type not allowed. File must be of type {allowed}".format(
self.error[
"file"
] = "File type not allowed. File must be of type {allowed}".format(
allowed=ALLOWED_IMAGE_FILES
)

12 changes: 6 additions & 6 deletions autofocus/predict/app/requests/PredictZipRequestValidator.py
Original file line number Diff line number Diff line change
@@ -3,9 +3,7 @@


class PredictZipRequestValidator(Validator):
"""
Validate request for endpoint predict_zip
"""
"""Validate request for endpoint predict_zip"""

def validate(self):
"""
@@ -18,11 +16,13 @@ def validate(self):
"""
self.error = {}

file = self.request.files.get('file', None)
file = self.request.files.get("file", None)
if not file:
self.error['file'] = "No file given."
self.error["file"] = "No file given."
elif not allowed_file(file.filename, ALLOWED_ZIP_FILES):
self.error['file'] = "File type not allowed. File must be of type {allowed}".format(
self.error[
"file"
] = "File type not allowed. File must be of type {allowed}".format(
allowed=ALLOWED_ZIP_FILES
)

20 changes: 9 additions & 11 deletions autofocus/predict/app/requests/Validator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABC, abstractmethod

from flask import abort, jsonify, make_response
from flask_api import status

@@ -48,15 +49,12 @@ def getError(self):
dict: The errors found during validation
"""
return self.error

def abort(self):
"""
Abort with errors
"""
abort(make_response(
jsonify(
status=status.HTTP_400_BAD_REQUEST,
error=self.getError()
),
status.HTTP_400_BAD_REQUEST
))
"""Abort with errors"""
abort(
make_response(
jsonify(status=status.HTTP_400_BAD_REQUEST, error=self.getError()),
status.HTTP_400_BAD_REQUEST,
)
)