Skip to content

fix(chen2021Fitness): don't hand out invalid JSON when there are not enough data to compute a fit #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions ext_models/chen2021Fitness/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def fit(
# adapt the data to the required format
x = t
x = sm.add_constant(x)
y = np.column_stack((k, n-k))
y = np.column_stack((k, n - k))

# estimate the model
model = sm.GLM(y, x, family=sm.families.Binomial(link=sm.families.links.logit())).fit(disp=0)

Expand All @@ -32,6 +32,10 @@ def fit(

# take the MLE of the parameters as the functions of the MLE of beta0, beta
beta0, beta1 = model.params

if beta1 == 0:
raise ValueError("Cannot compute fit: beta1 is zero. Did you supply enough data points?")

t0, a, fd, fc = -beta0 / beta1, \
beta1, \
np.exp(beta1 * generation_time) - 1, \
Expand Down
7 changes: 5 additions & 2 deletions ext_models/chen2021Fitness/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@


@app.route("/", methods=["POST"])
def without_prediction() -> Dict:
return process(request.json)
def without_prediction():
try:
return process(request.json)
except Exception as e:
return {"error": str(e)}, 500
36 changes: 36 additions & 0 deletions src/main/kotlin/ch/ethz/covspectrum/controller/ExceptionHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.ethz.covspectrum.controller

import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.client.HttpStatusCodeException
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

@ControllerAdvice
class ExceptionHandler : ResponseEntityExceptionHandler() {
@ExceptionHandler(Throwable::class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
fun handleUnexpectedException(e: Throwable): ResponseEntity<ErrorResponse> {
return ResponseEntity(
ErrorResponse(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
HttpStatus.INTERNAL_SERVER_ERROR.name,
e.message ?: "An unexpected error occurred"
),
HttpStatus.INTERNAL_SERVER_ERROR
)
}

@ExceptionHandler(HttpStatusCodeException::class)
fun handleBadRequestException(e: HttpStatusCodeException): ResponseEntity<String> {
return ResponseEntity(e.responseBodyAsString, e.statusCode)
}
}

data class ErrorResponse(
val status: Int,
val error: String,
val message: String
)
Loading