backend/model_api/views.py
def predict(request):
    """
    This endpoint handles predicting the data for a specific area over some number
    of days in the future. The expected query params are "country", "state" and "days",
    and the response is a list of data points, each represented as custom objects containing
    the date of infection, the value, and source (a string indicating whether this data point was
    predicted by our model or is from observed data) in a given number of future days.
    """
    country = request.query_params.get("country")
    state = request.query_params.get("state")
    days = int(request.query_params.get("days"))

    true_vals = ['True', 'true']
    current = request.query_params.get("current", None) in true_vals
    worst_effort = request.query_params.get("worst_effort", None) in true_vals
    best_effort = request.query_params.get("best_effort", None) in true_vals

    # Get the models from the URl query parameters.
    # Django expects it to be of the form: 'models=foo&models=bar&...'
    models = request.GET.getlist("models[]", [])

    # Get the area and raise an API exception if we can't.
    try:
        area = Area.objects.get(country=country, state=state)
    except Area.DoesNotExist:
        msg = "Could not find the area for country '{0}'".format(country)
        if state:
            msg += " and state '{0}'".format(state)
        raise APIException(msg)
    except Area.MultipleObjectsReturned:
        msg = "Found multiple areas for country '{0}'".format(country)
        if state:
            msg += " and state '{0}'".format(state)
        msg += ". This is most likely an error with data cleansing."
        raise APIException(msg)

    response = {
        "observed": [],
        "predictions": [],
        "observed_deaths": [],
    }

    # Pull observed data from database.
    observed = Covid19DataPoint.objects.filter(area=area)
    for d in observed:
        response["observed"].append({
            "date": d.date,
            "value": d.val,
        })

    observed_deaths = Covid19DeathDataPoint.objects.filter(area=area)
    for d in observed_deaths:
        response["observed_deaths"].append({
            "date": d.date,
            "value": d.val,
        })

    # Determine the time range for predictions.
    prediction_start_date = max([d.date for d in observed]) + timedelta(days=1)
    prediction_end_date = prediction_start_date + timedelta(days=days)

    for model_name in models:
        model = Covid19Model.objects.get(name=model_name)
        model_death = None
        if model_name[:10] == "SI-kJalpha":
            print(model_name + " (death prediction)")
            model_death = Covid19Model.objects.get(name=model_name + " (death prediction)")
        if current:
            qs = Covid19PredictionDataPoint.objects.filter(
                model=model,
                social_distancing=1,
                area=area,
                date__range=(prediction_start_date, prediction_end_date)
            )

            if model_death:
                qs_death = Covid19PredictionDataPoint.objects.filter(
                        model=model_death,
                        social_distancing=1,
                        area=area,
                        date__range=(prediction_start_date, prediction_end_date)
                )

            response["predictions"].append({
                "model": {
                    "name": model.name,
                    "description": model.description
                },
                "distancing": "current",
                "time_series": [{
                    "date": d.date,
                    "value": d.val,
                } for d in qs]
            })

            if model_death:
                response["predictions"].append({
                    "model": {
                        "name": model_death.name,
                        "description": model_death.description
                    },
                    "distancing": "current",
                    "time_series": [{
                        "date": d.date,
                        "value": d.val,
                    } for d in qs_death]
                })

        if worst_effort:
            qs = Covid19PredictionDataPoint.objects.filter(
                model=model,
                social_distancing=0,
                area=area,
                date__range=(prediction_start_date, prediction_end_date)
            )

            if model_death:
                qs_death = Covid19PredictionDataPoint.objects.filter(
                    model=model_death,
                    social_distancing=0,
                    area=area,
                    date__range=(prediction_start_date, prediction_end_date)
                )

            response["predictions"].append({
                "model": {
                    "name": model.name,
                    "description": model.description
                },
                "distancing": "worst_effort",
                "time_series": [{
                    "date": d.date,
                    "value": d.val,
                } for d in qs]
            })

            if model_death:
                response["predictions"].append({
                    "model": {
                        "name": model_death.name,
                        "description": model_death.description
                    },
                    "distancing": "worst_effort",
                    "time_series": [{
                        "date": d.date,
                        "value": d.val,
                    } for d in qs_death]
                })

        if best_effort:
            qs = Covid19PredictionDataPoint.objects.filter(
                model=model,
                social_distancing=2,
                area=area,
                date__range=(prediction_start_date, prediction_end_date)
            )

            if model_death:
                qs_death = Covid19PredictionDataPoint.objects.filter(
                    model=model_death,
                    social_distancing=2,
                    area=area,
                    date__range=(prediction_start_date, prediction_end_date)
                )

            response["predictions"].append({
                "model": {
                    "name": model.name,
                    "description": model.description
                },
                "distancing": "best_effort",
                "time_series": [{
                    "date": d.date,
                    "value": d.val,
                } for d in qs]
            })

            if model_death:
                response["predictions"].append({
                    "model": {
                        "name": model_death.name,
                        "description": model_death.description
                    },
                    "distancing": "best_effort",
                    "time_series": [{
                        "date": d.date,
                        "value": d.val,
                    } for d in qs_death]
                })


    return Response(response)

    This endpoint handles predicting the data for a specific area over some number
    of days in the future. The expected query params are "country", "state" and "days",
    and the response is a list of data points, each represented as custom objects containing
    the date of infection, the value, and source (a string indicating whether this data point was
    predicted by our model or is from observed data) in a given number of future days.
    
This endpoint handles predicting the data for a specific area over some number
of days in the future.
This endpoint handles predicting the data for a specific area over some number of days in the future .
def predict(request):
    country = request.query_params.get("country")
    state = request.query_params.get("state")
    days = int(request.query_params.get("days"))
    true_vals = ['True', 'true']
    current = request.query_params.get("current", None) in true_vals
    worst_effort = request.query_params.get("worst_effort", None) in true_vals
    best_effort = request.query_params.get("best_effort", None) in true_vals
    models = request.GET.getlist("models[]", [])
    try:
        area = Area.objects.get(country=country, state=state)
    except Area.DoesNotExist:
        msg = "Could not find the area for country '{0}'".format(country)
        if state:
            msg += " and state '{0}'".format(state)
        raise APIException(msg)
    except Area.MultipleObjectsReturned:
        msg = "Found multiple areas for country '{0}'".format(country)
        if state:
            msg += " and state '{0}'".format(state)
        msg += ". This is most likely an error with data cleansing."
        raise APIException(msg)
    response = {
        "observed": [],
        "predictions": [],
        "observed_deaths": [],
    }
    observed = Covid19DataPoint.objects.filter(area=area)
    for d in observed:
        response["observed"].append({
            "date": d.date,
            "value": d.val,
        })
    observed_deaths = Covid19DeathDataPoint.objects.filter(area=area)
    for d in observed_deaths:
        response["observed_deaths"].append({
            "date": d.date,
            "value": d.val,
        })
    prediction_start_date = max([d.date for d in observed]) + timedelta(days=1)
    prediction_end_date = prediction_start_date + timedelta(days=days)
    for model_name in models:
        model = Covid19Model.objects.get(name=model_name)
        model_death = None
        if model_name[:10] == "SI-kJalpha":
            print(model_name + " (death prediction)")
            model_death = Covid19Model.objects.get(name=model_name + " (death prediction)")
        if current:
            qs = Covid19PredictionDataPoint.objects.filter(
                model=model,
                social_distancing=1,
                area=area,
                date__range=(prediction_start_date, prediction_end_date)
            )
            if model_death:
                qs_death = Covid19PredictionDataPoint.objects.filter(
                        model=model_death,
                        social_distancing=1,
                        area=area,
                        date__range=(prediction_start_date, prediction_end_date)
                )
            response["predictions"].append({
                "model": {
                    "name": model.name,
                    "description": model.description
                },
                "distancing": "current",
                "time_series": [{
                    "date": d.date,
                    "value": d.val,
                } for d in qs]
            })
            if model_death:
                response["predictions"].append({
                    "model": {
                        "name": model_death.name,
                        "description": model_death.description
                    },
                    "distancing": "current",
                    "time_series": [{
                        "date": d.date,
                        "value": d.val,
                    } for d in qs_death]
                })
        if worst_effort:
            qs = Covid19PredictionDataPoint.objects.filter(
                model=model,
                social_distancing=0,
                area=area,
                date__range=(prediction_start_date, prediction_end_date)
            )
            if model_death:
                qs_death = Covid19PredictionDataPoint.objects.filter(
                    model=model_death,
                    social_distancing=0,
                    area=area,
                    date__range=(prediction_start_date, prediction_end_date)
                )
            response["predictions"].append({
                "model": {
                    "name": model.name,
                    "description": model.description
                },
                "distancing": "worst_effort",
                "time_series": [{
                    "date": d.date,
                    "value": d.val,
                } for d in qs]
            })
            if model_death:
                response["predictions"].append({
                    "model": {
                        "name": model_death.name,
                        "description": model_death.description
                    },
                    "distancing": "worst_effort",
                    "time_series": [{
                        "date": d.date,
                        "value": d.val,
                    } for d in qs_death]
                })
        if best_effort:
            qs = Covid19PredictionDataPoint.objects.filter(
                model=model,
                social_distancing=2,
                area=area,
                date__range=(prediction_start_date, prediction_end_date)
            )
            if model_death:
                qs_death = Covid19PredictionDataPoint.objects.filter(
                    model=model_death,
                    social_distancing=2,
                    area=area,
                    date__range=(prediction_start_date, prediction_end_date)
                )
            response["predictions"].append({
                "model": {
                    "name": model.name,
                    "description": model.description
                },
                "distancing": "best_effort",
                "time_series": [{
                    "date": d.date,
                    "value": d.val,
                } for d in qs]
            })
            if model_death:
                response["predictions"].append({
                    "model": {
                        "name": model_death.name,
                        "description": model_death.description
                    },
                    "distancing": "best_effort",
                    "time_series": [{
                        "date": d.date,
                        "value": d.val,
                    } for d in qs_death]
                })
    return Response(response)
def predict ( request ) : country = request . query_params . get ( "country" ) state = request . query_params . get ( "state" ) days = int ( request . query_params . get ( "days" ) ) true_vals = [ 'True' , 'true' ] current = request . query_params . get ( "current" , None ) in true_vals worst_effort = request . query_params . get ( "worst_effort" , None ) in true_vals best_effort = request . query_params . get ( "best_effort" , None ) in true_vals models = request . GET . getlist ( "models[]" , [ ] ) try : area = Area . objects . get ( country = country , state = state ) except Area . DoesNotExist : msg = "Could not find the area for country '{0}'" . format ( country ) if state : msg += " and state '{0}'" . format ( state ) raise APIException ( msg ) except Area . MultipleObjectsReturned : msg = "Found multiple areas for country '{0}'" . format ( country ) if state : msg += " and state '{0}'" . format ( state ) msg += ". This is most likely an error with data cleansing." raise APIException ( msg ) response = { "observed" : [ ] , "predictions" : [ ] , "observed_deaths" : [ ] , } observed = Covid19DataPoint . objects . filter ( area = area ) for d in observed : response [ "observed" ] . append ( { "date" : d . date , "value" : d . val , } ) observed_deaths = Covid19DeathDataPoint . objects . filter ( area = area ) for d in observed_deaths : response [ "observed_deaths" ] . append ( { "date" : d . date , "value" : d . val , } ) prediction_start_date = max ( [ d . date for d in observed ] ) + timedelta ( days = 1 ) prediction_end_date = prediction_start_date + timedelta ( days = days ) for model_name in models : model = Covid19Model . objects . get ( name = model_name ) model_death = None if model_name [ : 10 ] == "SI-kJalpha" : print ( model_name + " (death prediction)" ) model_death = Covid19Model . objects . get ( name = model_name + " (death prediction)" ) if current : qs = Covid19PredictionDataPoint . objects . filter ( model = model , social_distancing = 1 , area = area , date__range = ( prediction_start_date , prediction_end_date ) ) if model_death : qs_death = Covid19PredictionDataPoint . objects . filter ( model = model_death , social_distancing = 1 , area = area , date__range = ( prediction_start_date , prediction_end_date ) ) response [ "predictions" ] . append ( { "model" : { "name" : model . name , "description" : model . description } , "distancing" : "current" , "time_series" : [ { "date" : d . date , "value" : d . val , } for d in qs ] } ) if model_death : response [ "predictions" ] . append ( { "model" : { "name" : model_death . name , "description" : model_death . description } , "distancing" : "current" , "time_series" : [ { "date" : d . date , "value" : d . val , } for d in qs_death ] } ) if worst_effort : qs = Covid19PredictionDataPoint . objects . filter ( model = model , social_distancing = 0 , area = area , date__range = ( prediction_start_date , prediction_end_date ) ) if model_death : qs_death = Covid19PredictionDataPoint . objects . filter ( model = model_death , social_distancing = 0 , area = area , date__range = ( prediction_start_date , prediction_end_date ) ) response [ "predictions" ] . append ( { "model" : { "name" : model . name , "description" : model . description } , "distancing" : "worst_effort" , "time_series" : [ { "date" : d . date , "value" : d . val , } for d in qs ] } ) if model_death : response [ "predictions" ] . append ( { "model" : { "name" : model_death . name , "description" : model_death . description } , "distancing" : "worst_effort" , "time_series" : [ { "date" : d . date , "value" : d . val , } for d in qs_death ] } ) if best_effort : qs = Covid19PredictionDataPoint . objects . filter ( model = model , social_distancing = 2 , area = area , date__range = ( prediction_start_date , prediction_end_date ) ) if model_death : qs_death = Covid19PredictionDataPoint . objects . filter ( model = model_death , social_distancing = 2 , area = area , date__range = ( prediction_start_date , prediction_end_date ) ) response [ "predictions" ] . append ( { "model" : { "name" : model . name , "description" : model . description } , "distancing" : "best_effort" , "time_series" : [ { "date" : d . date , "value" : d . val , } for d in qs ] } ) if model_death : response [ "predictions" ] . append ( { "model" : { "name" : model_death . name , "description" : model_death . description } , "distancing" : "best_effort" , "time_series" : [ { "date" : d . date , "value" : d . val , } for d in qs_death ] } ) return Response ( response )
This endpoint handles predicting the data for a specific area over some number
of days in the future.
This endpoint handles predicting the data for a specific area over some number of days in the future .
"""
    This endpoint handles predicting the data for a specific area over some number
    of days in the future. The expected query params are "country", "state" and "days",
    and the response is a list of data points, each represented as custom objects containing
    the date of infection, the value, and source (a string indicating whether this data point was
    predicted by our model or is from observed data) in a given number of future days.
    """ # Get the models from the URl query parameters. # Django expects it to be of the form: 'models=foo&models=bar&...' # Get the area and raise an API exception if we can't. # Pull observed data from database. # Determine the time range for predictions.
request