Ejemplo n.º 1
0
def test_year_outliers(years=[2000, 2005, 2010], outliers_mode=None):
    list_iso3 = read_list()
    for iso3 in list_iso3:
        print iso3, '\n', '==='
        data = extract_data(iso3)[0]
        missing = extract_missing(data)
        for year in years:
            print 'starting year:', year 
            # If the starting year is valid
            if ut._verify_first_year(year, data, missing, T):
                if outliers_mode == 'random':
                    n_outliers = random.choice(range(1, (2014 - year)/2))
                    outliers = random.sample(range(year, 2014), n_outliers)
                else:
                    outliers = None
                print 'outliers:', outliers
                if outliers and min(outliers) < year:
                    print e.flash_outliers()
                if outliers and (year in outliers):
                    print e.error_outl()
                    enough_data = False
                else:
                    if outliers:
                        outliers = [(n, 1) for n in outliers]
                    # Select the data for the projection
                    enough_data, sel_data, figdata = ed._select_data((year,1), data,
                            outliers=outliers)
            else:
                enough_data = False
                print e.error_year(T)
        # If the data is not enough to do time series smoothing ...
        if not enough_data:
            print e.error_not_enough()
    return
Ejemplo n.º 2
0
def test_projection():
    list_iso3 = read_list()
    for iso3 in list_iso3:
        print iso3, '\n', '==='
        extracted = extract_data(iso3)
        data = extracted[0]
        prev_100_k = extracted[6]
        times, _ = zip(*data['times'])
        year_st = times[0]
        year = times[-1] + 1
        outliers = []
        enough_data, sel_data, figdata = ed._select_data((year_st,1), data,
                outliers=outliers)
        pop = extracted[9]
        if enough_data:
            length = random.choice([5])
            under_report = random.choice([1, 3, 6])
            start_report = random.choice(range(year, 2025))
            end_report = random.choice(range(start_report, 2025))
            final_under_report = 0.1
            found_min = random.choice(range(50, 500 ))
            found_max = random.choice(range(found_min, 500 ))
            start_finding = random.choice(range(year, 2025))
            end_finding = random.choice(range(start_finding, 2025))
            improve_hc = random.choice([0.01, 0.10])
            exp_dec_min = random.choice([2, 3, 4])/100.0
            exp_dec_max = exp_dec_min - random.choice(range(3))/100.0
            proj_times, exp_smooth, est_notif_min, est_notif_max, figdata = \
pr.projection(sel_data, pop, length_project = length, 
            periods=1, prev_100_k=prev_100_k,
            under_report=under_report, final_under_report=final_under_report,
            start_report=start_report, end_report=end_report,
            found_min=found_min, found_max=found_max,
            start_finding=start_finding, end_finding=end_finding, 
            improve_hc = improve_hc, 
            exp_dec_min=exp_dec_min, exp_dec_max=exp_dec_max)
            print (under_report, final_under_report, start_report, end_report,
            found_min, found_max, start_finding, end_finding, improve_hc,
            exp_dec_min, exp_dec_max)
            print 'min', est_notif_min
            print 'max', est_notif_max
            assert min(est_notif_min + est_notif_max) >= 0

            print 'Test +hc'
            proj_times, exp_smooth, est_notif_min, est_notif_max, figdata = \
pr.projection(sel_data, pop, length_project = length, 
            periods=1, prev_100_k=prev_100_k,
            under_report=under_report, final_under_report=final_under_report,
            start_report=start_report, end_report=end_report,
            found_min=found_min, found_max=found_max,
            start_finding=start_finding, end_finding=end_finding, 
            improve_hc = improve_hc + 0.01, 
            exp_dec_min=exp_dec_min, exp_dec_max=exp_dec_max)
            print 'min', est_notif_min
            print 'max', est_notif_max
            assert min(est_notif_min + est_notif_max) >= 0

            print 'Test +underreport'
            proj_times, exp_smooth, est_notif_min, est_notif_max, figdata = \
pr.projection(sel_data, pop, length_project = length, 
            periods=1, prev_100_k=prev_100_k,
            under_report=under_report + 2.0, final_under_report=final_under_report,
            start_report=start_report, end_report=end_report,
            found_min=found_min, found_max=found_max,
            start_finding=start_finding, end_finding=end_finding, 
            improve_hc = improve_hc, 
            exp_dec_min=exp_dec_min, exp_dec_max=exp_dec_max)
            print 'min', est_notif_min
            print 'max', est_notif_max
            assert min(est_notif_min + est_notif_max) >= 0

            print 'Test final_underreport'
            proj_times, exp_smooth, est_notif_min, est_notif_max, figdata = \
pr.projection(sel_data, pop, length_project = length, 
            periods=1, prev_100_k=prev_100_k,
            under_report=under_report, final_under_report=under_report,
            start_report=start_report, end_report=end_report,
            found_min=found_min, found_max=found_max,
            start_finding=start_finding, end_finding=end_finding, 
            improve_hc = improve_hc, 
            exp_dec_min=exp_dec_min, exp_dec_max=exp_dec_max)
            print 'min', est_notif_min
            print 'max', est_notif_max
            assert min(est_notif_min + est_notif_max) >= 0

            print 'Test found_min - found_max'
            proj_times, exp_smooth, est_notif_min, est_notif_max, figdata = \
pr.projection(sel_data, pop, length_project = length, 
            periods=1, prev_100_k=prev_100_k,
            under_report=under_report, final_under_report=final_under_report,
            start_report=start_report, end_report=end_report,
            found_min=found_min + 50, found_max=found_max+50,
            start_finding=start_finding, end_finding=end_finding, 
            improve_hc = improve_hc, 
            exp_dec_min=exp_dec_min, exp_dec_max=exp_dec_max)
            print 'min', est_notif_min
            print 'max', est_notif_max
            assert min(est_notif_min + est_notif_max) >= 0


            print 'Test found_max'
            proj_times, exp_smooth, est_notif_min, est_notif_max, figdata = \
pr.projection(sel_data, pop, length_project = length, 
            periods=1, prev_100_k=prev_100_k,
            under_report=under_report, final_under_report=final_under_report,
            start_report=start_report, end_report=end_report,
            found_min=found_min, found_max=found_max+200,
            start_finding=start_finding, end_finding=end_finding, 
            improve_hc = improve_hc, 
            exp_dec_min=exp_dec_min, exp_dec_max=exp_dec_max)
            print 'min', est_notif_min
            print 'max', est_notif_max
            assert min(est_notif_min + est_notif_max) >= 0
    return