예제 #1
0
def data_inserted():

    abs_notif = []
    times = []
    
    # usual issue of int changed to unicode str in session dict...
    pop = {int(k): session['pop'][k] for k in session['pop']}

    # we also want to check that there is enough recent data
    recent_data = 0

    for y in session['hist_years']:
        for i in session['columns']:
            if ut.is_int(request.form["%s_%s" %(y, i)]) and pop[y] != 0:
                times.append((int(y), i))
                abs_notif.append(int(request.form["%s_%s" %(y, i)]))
                if y >= SEC_LAST:
                    recent_data += 1

    # Check if enough recent data: 
    # e.g. for quarterly it means at least 4 quarters after SEC_LAST year 
    if recent_data < session['periods'] or len(times) < T:
        return render_template("error.html", error=e.error_not_recent(T,
                SEC_LAST))
    
    # arrange the data in lists
    periods = session['periods']
    data = {'years': times, 'abs_notif': abs_notif}
    session['data'] = ed.arrange_data(data, pop, periods=periods)
    abs_notif = [int(n) for n in session['data']['abs_notif']]

    # plot it
    figdata = ed._plot_time_series(session['data'], periods=periods)

    # Display notification data
    to_display = zip(session['data']['times'], abs_notif)

    # Prepare empty rows for all years and then fill them
    hist = {y: [' ' for p in range(session['periods'])] for y in 
            range(to_display[0][0][0], to_display[-1][0][0]+1)}
    for t, n in to_display:
        hist[t[0]][t[1]-1] = n
    
    #column headers
    if periods == 1:
        columns = ['Notifications']
    elif periods == 4:
        columns = ['%d' %i for i in range(1,5)]
    elif periods == 12:
        columns = ['%d' %i for i in range(1,13)]

    return render_template("inserted_data.html", fig=figdata, hist=hist, 
            columns=columns)
예제 #2
0
def display_data():
    
    # read population as inputed by the user
    session['pop'] = {int(k): int(v) for (k, v) in session['pop'].items()}
    for y in session['all_years']:
        if ut.is_int(request.form['pop_%d' %y]):
            session['pop'][y] = int(request.form['pop_%d' %y])
        else: 
           return render_template("error.html", error="Invalid format for \
population size in year %d" %y)

    if session['manual'] == True:
        # for yearly input, tables will have two columns
        half = int(math.ceil(len(session['hist_years'])/2.0))
        return render_template("input_table.html", columns=session['columns'], 
                years=session['hist_years'], half=half)

    # compute relative change per year, notif rates, etc 
    session['data'] = ed.arrange_data(session['all_data'], session['pop'], periods=1)

    # copy values except those in newdata
    for k in ['decline', 'detected_mdr', 'total_mdr', 'p_ret_rel', 'p_new', 
            'prob_mdr_new', 'prob_mdr_re', 'prev_100_k']:
        session[k] = session['all_data'][k]

    #plot the data
    figdata = ed._plot_time_series(session['data'])
        
    #produce list of years for which data is missing (does not include
    #years after X if data is missing for all years after X)
    if session['data']['interpolated']:
        missing_data, _ = zip(*session['data']['interpolated'])
        session['missing'], _ = zip(*missing_data)
    else: 
        session['missing'] = []
        
    #extract notifications for last 10 years to display them
    years, _ = zip(*session['data']['times'])
    abs_notif = [int(n) for n in session['data']['abs_notif']]
    years_notif = zip(years, abs_notif)[-min(10, len(abs_notif)):]
    # first data point available
    year = years[0]
    return render_template("display_data.html", country=session['country'], 
            fig=figdata, missing=session['missing'], year=year,
            years_notif=years_notif)
예제 #3
0
def data_inserted_2():

    abs_notif = []
    times = []
    
    # usual issue of int changed to unicode str in session dict...
    pop = {int(k): session['pop'][k] for k in session['pop']}

    # we also want to check that there is enough recent data
    recent_data = 0

    # Get the name of the uploaded file
    f = request.files['file']
    
    # Check if the file is one of the allowed types/extensions
    if allowed_file(f.filename):
        # Make the filename safe, remove unsupported chars
        filename = secure_filename(f.filename)
        # Save the file
        f.save(filename)
    else:
        return render_template('error.html', error = 'The uploaded file does \
not have csv extension.')
    try:
        with open(f.filename, 'r') as g:
            df = pd.read_csv(g)
    except: #for all possible errors...
        return render_template('error.html', error = 'No file loaded or \
incorrect format.')
            
    if session['periods'] == 1:
        headers = ['year', '1']
    if session['periods'] == 4:
        headers = ['year', '1', '2', '3', '4']
    if session['periods'] == 12:
        headers = ['year', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
                '11', '12']
    df_headers = list(df.columns)
    if len(headers) != len(df_headers):
       return render_template('error.html', error = 'Unexpected number of \
columns in the uploaded file!')
    for i in range(len(headers)):
        if headers[i] != df_headers[i]:
            return render_template('error.html', error = 'Some column header \
does not have the right format!')

    df_years = list(df.year)
    for y in session['hist_years']:
        for i in session['columns']:
            if y in df_years and ut.is_int(df[df['year']==y].iloc[0]['%s' %i]) and pop[y] != 0:
                times.append((int(y), i))
                abs_notif.append(int(df[df['year']==y].iloc[0]['%s' %i]))
                if y >= SEC_LAST:
                    recent_data += 1
    os.remove(filename)

    # Check if enough recent data: 
    # e.g. for quarterly it means at least 4 quarters after SEC_LAST year 
    if recent_data < session['periods'] or len(times) < T:
        return render_template("error.html", error=e.error_not_recent(T,
                SEC_LAST))
    
    # arrange the data in lists
    periods = session['periods']
    data = {'years': times, 'abs_notif': abs_notif}
    session['data'] = ed.arrange_data(data, pop, periods=periods)
    abs_notif = [int(n) for n in session['data']['abs_notif']]

    # plot it
    figdata = ed._plot_time_series(session['data'], periods=periods)

    # Display notification data
    to_display = zip(session['data']['times'], abs_notif)

    # Prepare empty rows for all years and then fill them
    hist = {y: [' ' for p in range(session['periods'])] for y in 
            range(to_display[0][0][0], to_display[-1][0][0]+1)}
    for t, n in to_display:
        hist[t[0]][t[1]-1] = n
    
    #column headers
    if periods == 1:
        columns = ['Notifications']
    elif periods == 4:
        columns = ['%d' %i for i in range(1,5)]
    elif periods == 12:
        columns = ['%d' %i for i in range(1,13)]

    return render_template("inserted_data.html", fig=figdata, hist=hist, 
            columns=columns)