backend/model_api/migrations/0008_load_predictions.py
def load_csv(apps, path):
    """
    Reads the given CSV and returns a list of Covid19PredictionDataPoint
    objects. Only the date, area, and val fields on the objects are set. Note
    that the objects have not been saved into the database yet.
    :param apps: Django apps object.
    :param path: Path to CSV with prediction data.
    :return: List of Covid19PredictionDataPoint objects (NOT SAVED YET).
    """
    Area = apps.get_model('model_api', 'Area')
    Covid19PredictionDataPoint = apps.get_model(
        'model_api', 'Covid19PredictionDataPoint')

    with open(path, 'r') as f:
        reader = csv.reader(f)
        header = next(reader, None)

        data = []

        for row in reader:
            area = None

            # Determine the country / state.
            if 'global' in path:
                country = row[1]
                state = ''
            elif 'us' in path:
                country = 'US'
                state = row[1]
            else:
                msg = """Could not determine country/state from:
                                    row = %s
                                    path = %s""" % (str(row), path)
                raise RuntimeError(msg)

            # Try to find the corresponding area.
            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)
                area = Area(state=state, country=country)
                area.save()
                msg += ' in model_api_area. New area created.'
                print(msg)

            except Area.MultipleObjectsReturned:
                msg = "Found multiple areas for country '{0}'".format(
                    country)
                if state:
                    msg += " and state '{0}'".format(state)
                msg += ' in model_api_area. Skip this area.'
                print(msg)
                continue

            # Load the predictions.
            for i in range(2, len(header)):
                date = header[i]

                # Skip invalid values.
                raw_val = row[i]
                if raw_val in ['NaN', '-Inf', 'Inf']:
                    continue

                # Skip negative values.
                val = int(float(raw_val))
                if val < 0:
                    continue

                data.append(Covid19PredictionDataPoint(
                    area=area,
                    date=date,
                    val=val,
                ))

        return data

    Reads the given CSV and returns a list of Covid19PredictionDataPoint
    objects. Only the date, area, and val fields on the objects are set. Note
    that the objects have not been saved into the database yet.
    :param apps: Django apps object.
    :param path: Path to CSV with prediction data.
    :return: List of Covid19PredictionDataPoint objects (NOT SAVED YET).
    
Reads the given CSV and returns a list of Covid19PredictionDataPoint
objects. Only the date, area, and val fields on the objects are set. Note
that the objects have not been saved into the database yet.
Reads the given CSV and returns a list of Covid19PredictionDataPoint objects . Only the date area and val fields on the objects are set . Note that the objects have not been saved into the database yet .
def load_csv(apps, path):
    Area = apps.get_model('model_api', 'Area')
    Covid19PredictionDataPoint = apps.get_model(
        'model_api', 'Covid19PredictionDataPoint')
    with open(path, 'r') as f:
        reader = csv.reader(f)
        header = next(reader, None)
        data = []
        for row in reader:
            area = None
            if 'global' in path:
                country = row[1]
                state = ''
            elif 'us' in path:
                country = 'US'
                state = row[1]
            else:
                msg = """Could not determine country/state from:
                                    row = %s
                                    path = %s""" % (str(row), path)
                raise RuntimeError(msg)
            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)
                area = Area(state=state, country=country)
                area.save()
                msg += ' in model_api_area. New area created.'
                print(msg)
            except Area.MultipleObjectsReturned:
                msg = "Found multiple areas for country '{0}'".format(
                    country)
                if state:
                    msg += " and state '{0}'".format(state)
                msg += ' in model_api_area. Skip this area.'
                print(msg)
                continue
            for i in range(2, len(header)):
                date = header[i]
                raw_val = row[i]
                if raw_val in ['NaN', '-Inf', 'Inf']:
                    continue
                val = int(float(raw_val))
                if val < 0:
                    continue
                data.append(Covid19PredictionDataPoint(
                    area=area,
                    date=date,
                    val=val,
                ))
        return data
def load_csv ( apps , path ) : Area = apps . get_model ( 'model_api' , 'Area' ) Covid19PredictionDataPoint = apps . get_model ( 'model_api' , 'Covid19PredictionDataPoint' ) with open ( path , 'r' ) as f : reader = csv . reader ( f ) header = next ( reader , None ) data = [ ] for row in reader : area = None if 'global' in path : country = row [ 1 ] state = '' elif 'us' in path : country = 'US' state = row [ 1 ] else : msg = """Could not determine country/state from:
                                    row = %s
                                    path = %s""" % ( str ( row ) , path ) raise RuntimeError ( msg ) 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 ) area = Area ( state = state , country = country ) area . save ( ) msg += ' in model_api_area. New area created.' print ( msg ) except Area . MultipleObjectsReturned : msg = "Found multiple areas for country '{0}'" . format ( country ) if state : msg += " and state '{0}'" . format ( state ) msg += ' in model_api_area. Skip this area.' print ( msg ) continue for i in range ( 2 , len ( header ) ) : date = header [ i ] raw_val = row [ i ] if raw_val in [ 'NaN' , '-Inf' , 'Inf' ] : continue val = int ( float ( raw_val ) ) if val < 0 : continue data . append ( Covid19PredictionDataPoint ( area = area , date = date , val = val , ) ) return data
Reads the given CSV and returns a list of Covid19PredictionDataPoint
objects.
Reads the given CSV and returns a list of Covid19PredictionDataPoint objects .
"""
    Reads the given CSV and returns a list of Covid19PredictionDataPoint
    objects. Only the date, area, and val fields on the objects are set. Note
    that the objects have not been saved into the database yet.
    :param apps: Django apps object.
    :param path: Path to CSV with prediction data.
    :return: List of Covid19PredictionDataPoint objects (NOT SAVED YET).
    """ # Determine the country / state. # Try to find the corresponding area. # Load the predictions. # Skip invalid values. # Skip negative values.
List of Covid19PredictionDataPoint objects (NOT SAVED YET).
List of Covid19PredictionDataPoint objects ( NOT SAVED YET ) .
apps
Django apps object.
Django apps object .
path
Path to CSV with prediction data.
Path to CSV with prediction data .