Skip to content

Commit

Permalink
fix(chen2021Fitness): don't hand out invalid JSON when there are not …
Browse files Browse the repository at this point in the history
…enough data to compute a fit (#42)

Some values were NaN or Infinity, which are no valid JSON tokens. Instead throw an error.

resolves #41
  • Loading branch information
fengelniederhammer authored Jan 17, 2025
1 parent f981d5a commit 1702603
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
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
)

0 comments on commit 1702603

Please sign in to comment.