backend/model_api/migrations/0002_load_data.py
def load_csv(apps, path, scope, type):
    """
    Reads the given CSV and returns a list of Covid19DataPoint/Covid19DeathDataPoint
    objects. 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.
    :param scope: The scope of the historical data, "GLOBAL" or "US"
    :param type: The type of the historical data, "INFECTION" or "DEATH"
    :return: List of Covid19DataPoint/Covid19DeathDataPoint objects (NOT SAVED YET).
    """
    Area = apps.get_model('model_api', 'Area')
    Covid19DataPoint = apps.get_model('model_api', 'Covid19DataPoint')
    Covid19DeathDataPoint = apps.get_model(
        'model_api', 'Covid19DeathDataPoint')

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

        data = []
        using_death_data_point = type == "DEATH"
        using_state_level = scope == "US"

        for row in reader:
            area = None

            # Determine the country / state.
            if not using_state_level:
                country = row[1]
                state = ''
            else:
                country = 'US'
                state = row[1]

            # Try to find the corresponding area.
            try:
                area = Area.objects.get(country=country, state=state)
            except Area.DoesNotExist:
                area = Area(state=state, country=country)
                area.save()
                msg = 'Created area ' + ((state + ' ') if state != '' else '') + country + '.'
                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 data.
            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

                if not using_death_data_point:
                    data.append(Covid19DataPoint(
                        area=area,
                        date=date,
                        val=val,
                    ))
                else:
                    data.append(Covid19DeathDataPoint(
                        area=area,
                        date=date,
                        val=val,
                    ))

    return data

    Reads the given CSV and returns a list of Covid19DataPoint/Covid19DeathDataPoint
    objects. 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.
    :param scope: The scope of the historical data, "GLOBAL" or "US"
    :param type: The type of the historical data, "INFECTION" or "DEATH"
    :return: List of Covid19DataPoint/Covid19DeathDataPoint objects (NOT SAVED YET).
    
Reads the given CSV and returns a list of Covid19DataPoint/Covid19DeathDataPoint
objects. 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 Covid19DataPoint / Covid19DeathDataPoint objects . 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, scope, type):
    Area = apps.get_model('model_api', 'Area')
    Covid19DataPoint = apps.get_model('model_api', 'Covid19DataPoint')
    Covid19DeathDataPoint = apps.get_model(
        'model_api', 'Covid19DeathDataPoint')
    with open(path, 'r') as f:
        reader = csv.reader(f)
        header = next(reader, None)
        data = []
        using_death_data_point = type == "DEATH"
        using_state_level = scope == "US"
        for row in reader:
            area = None
            if not using_state_level:
                country = row[1]
                state = ''
            else:
                country = 'US'
                state = row[1]
            try:
                area = Area.objects.get(country=country, state=state)
            except Area.DoesNotExist:
                area = Area(state=state, country=country)
                area.save()
                msg = 'Created area ' + ((state + ' ') if state != '' else '') + country + '.'
                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
                if not using_death_data_point:
                    data.append(Covid19DataPoint(
                        area=area,
                        date=date,
                        val=val,
                    ))
                else:
                    data.append(Covid19DeathDataPoint(
                        area=area,
                        date=date,
                        val=val,
                    ))
    return data
def load_csv ( apps , path , scope , type ) : Area = apps . get_model ( 'model_api' , 'Area' ) Covid19DataPoint = apps . get_model ( 'model_api' , 'Covid19DataPoint' ) Covid19DeathDataPoint = apps . get_model ( 'model_api' , 'Covid19DeathDataPoint' ) with open ( path , 'r' ) as f : reader = csv . reader ( f ) header = next ( reader , None ) data = [ ] using_death_data_point = type == "DEATH" using_state_level = scope == "US" for row in reader : area = None if not using_state_level : country = row [ 1 ] state = '' else : country = 'US' state = row [ 1 ] try : area = Area . objects . get ( country = country , state = state ) except Area . DoesNotExist : area = Area ( state = state , country = country ) area . save ( ) msg = 'Created area ' + ( ( state + ' ' ) if state != '' else '' ) + country + '.' 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 if not using_death_data_point : data . append ( Covid19DataPoint ( area = area , date = date , val = val , ) ) else : data . append ( Covid19DeathDataPoint ( area = area , date = date , val = val , ) ) return data
Reads the given CSV and returns a list of Covid19DataPoint/Covid19DeathDataPoint
objects.
Reads the given CSV and returns a list of Covid19DataPoint / Covid19DeathDataPoint objects .
"""
    Reads the given CSV and returns a list of Covid19DataPoint/Covid19DeathDataPoint
    objects. 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.
    :param scope: The scope of the historical data, "GLOBAL" or "US"
    :param type: The type of the historical data, "INFECTION" or "DEATH"
    :return: List of Covid19DataPoint/Covid19DeathDataPoint objects (NOT SAVED YET).
    """ # Determine the country / state. # Try to find the corresponding area. # Load the data. # Skip invalid values. # Skip negative values.
List of Covid19DataPoint/Covid19DeathDataPoint objects (NOT SAVED YET).
List of Covid19DataPoint / Covid19DeathDataPoint objects ( NOT SAVED YET ) .
apps
Django apps object.
Django apps object .
path
Path to CSV with prediction data.
Path to CSV with prediction data .
scope
The scope of the historical data, "GLOBAL" or "US"
The scope of the historical data " GLOBAL " or " US "
type
The type of the historical data, "INFECTION" or "DEATH"
The type of the historical data " INFECTION " or " DEATH "