Exemplo n.º 1
0
def estimate_pvalue_SI(underlying: Network,
                       theta: float,
                       outcome: dict,
                       initials: set,
                       immunes: set = set(),
                       tmax=1000.0,
                       dt=1,
                       n_iterations=10000):
    est_p = Simulator.estimate_SI(underlying=underlying,
                                  theta=theta,
                                  outcome=outcome,
                                  initials=initials,
                                  tmax=tmax,
                                  dt=dt)
    n_less = 0
    for i in range(n_iterations):
        result = Simulator.simulate_SI(underlying=underlying,
                                       theta=theta,
                                       infected=initials,
                                       immunes=immunes,
                                       tmax=tmax,
                                       dt=dt)
        if result['p'] <= est_p:
            n_less += 1
    return n_less / n_iterations
def test_SI_relic(niter=10, show=False):
    nm = test_network_manager()
    result = Simulator.simulate_SI_relic(underlying=nm.network,
                                         theta=0.05,
                                         relic=0.01,
                                         infected={1},
                                         tmax=300,
                                         dt=0.01)
    result['outcome'][1] = 0.0
    est.set_diffusion_data(nm.get_dos_filepath(),
                           counters=None,
                           outcome=result['outcome'])

    t0 = random.random() + 1e-12
    r0 = random.random() + 1e-12
    res = est.llmax_for_diffusion_SI_relic(t0, r0)
    print('Max: ' + str(res))
    print(result['outcome'])
    if show:
        thetas = np.arange(1e-12, 0.1, 0.001)
        relics = np.arange(1e-12, 0.1, 0.001)
        func = generate_ll_func_SI_relic(nm, result['outcome'], thetas, relics)
        pl_rend.show_likelyhood_heatmap2D(thetas,
                                          relics,
                                          func, [0.05, 0.01],
                                          [res['theta'], res['relic']],
                                          xlabel='theta',
                                          ylabel='relic',
                                          exp=True)
def test_SI(ntw: Network, true_theta: float):
    result = Simulator.simulate_SI(underlying=ntw,
                                   theta=true_theta,
                                   infected={1},
                                   tmax=300,
                                   dt=1)
    print(result)
    th = 0.0
    est_th = th
    best_pest = 0
    ml_pval = 0
    thetas = []
    ps = []
    total_pest = 0
    while th <= 1.0:
        pest = Simulator.estimate_SI(underlying=ntw,
                                     outcome=result['outcome'],
                                     theta=th,
                                     initials={1},
                                     tmax=300,
                                     dt=1)
        # print(str(th) + ' ' + str(pest))
        if pest > best_pest:
            best_pest = pest
            est_th = th
        thetas.append(th)
        ps.append(pest)
        total_pest += pest
        th += 0.01
    pval = de.estimate_pvalue_SI(underlying=ntw,
                                 outcome=result['outcome'],
                                 theta=est_th,
                                 initials={1},
                                 tmax=300,
                                 dt=1,
                                 n_iterations=1000)
    print('True theta: ' + str(true_theta))
    print('Max liklehood estimation: ' + str(est_th) + ' p-val ' + str(pval))
    psnorm = stat.normalize_series(ps)
    confide = stat.confidential_from_p_series(psnorm, 0.95)
    print('0.95 confidential interval: (' + str(thetas[confide[0]]) + ', ' +
          str(thetas[confide[1]]) + ')')

    pyplot.plot(thetas, psnorm)
    pyplot.show()
def test_ICM_libtest(netwrk: Network,
                     true_theta: float,
                     true_relic: float = .0,
                     single_point=False):
    netman = NetworkManager(net=netwrk,
                            name='temp',
                            base_dir='D:/BigData/Charity/Cascades/')
    netman.save_to_file()

    #result = Simulator.simulate_ICM(underlying=netwrk, theta=true_theta, relic=true_relic, infected={1})
    outcomes = []
    otimes = []
    for i in range(50):
        result = Simulator.simulate_SI_decay_confirm_relicexp_hetero(
            underlying=netwrk,
            theta=.1,
            relic=.0,
            confirm=.0,
            decay=.1,
            infected={1},
            tmax=300,
            dt=0.05)
        result['outcome'][1] = .0
        print(result)
        outcomes.append(result['outcome'])
        otimes.append(300)

    est.set_diffusion_data_ensemble_newlib(netman.get_dos_filepath(),
                                           outcomes=outcomes,
                                           observe_times=otimes,
                                           echo=False)
    est_th = .0
    best_pest = -99999999999
    total_pest = 0
    thetas = np.arange(0.01, 1.0, 0.001)
    ps = np.zeros(len(thetas))

    for i in range(len(thetas)):
        th = thetas[i]
        pest = math.exp(-est.loglikelyhood_ICM([th, .0]))
        print(pest)
        if pest > best_pest:
            best_pest = pest
            est_th = th
        ps[i] = pest
        total_pest += pest

    print('True theta: ' + str(true_theta))
    print('Max liklehood estimation: ' + str(est_th) + ' ' +
          str(-0.1 * math.log(1 - est_th)))
    psnorm = stat.normalize_series(list(ps))
    confide = stat.confidential_from_p_series(psnorm, 0.95)
    print('0.95 confidential interval: (' + str(thetas[confide[0]]) + ', ' +
          str(thetas[confide[1]]) + ')')

    pyplot.plot(thetas, psnorm)
    pyplot.show()
def test_convergence(niter=10):
    nm = test_network_manager()
    result = Simulator.simulate_SI_relic(underlying=nm.network,
                                         theta=0.1,
                                         relic=0.005,
                                         infected={1},
                                         tmax=300,
                                         dt=0.01)
    result['outcome'][1] = 0.0
    est.set_diffusion_data(nm.get_dos_filepath(),
                           counters=None,
                           outcome=result['outcome'])
    for i in range(niter):
        t0 = random.random() + 1e-12
        r0 = random.random() + 1e-12
        print('Max: ' + str(est.llmax_for_diffusion_SI_relic(t0, r0)))
def simulate_diffusion_cascades_zashitim_taigu(n_cascades, theta, rho, delta):
    casman = CascadesManager(name='Zashitim_taigu_-164443025_8726_8021_6846',
                             base_dir='D:/BigData/Charity/Cascades/')
    casman.load_from_file()
    cascade = casman.get_cascade_by_id(6846)
    print(cascade.get_minimum_delay())
    print(max(cascade.get_outcome(normalization_factor=3600).values()))
    est.set_alpha_for_node(-164443025, 25)
    outcome = Simulator.simulate_SI_decay_confirm_relicexp_hetero(
        casman.underlying_net.network,
        theta,
        delta,
        .0,
        rho,
        infected={-164443025},
        tmax=2500.,
        dt=30,
        echo=True)
    print(outcome)
def test_SI_relic_decay_libtest(ntw: Network,
                                true_theta: float,
                                true_relic: float,
                                newlib=False,
                                single_point=False):
    #test_outcome = {5: 11.89999999999979, 6: 36.75000000000126, 20: 39.49000000000071, 1: 39.65000000000068, 3: 40.0600000000006, 26: 40.840000000000444, 11: 41.31000000000035, 2: 43.799999999999855, 13: 44.299999999999756, 22: 45.64999999999949, 25: 46.29999999999936, 12: 46.33999999999935, 16: 47.43999999999913, 9: 49.53999999999871, 7: 49.68999999999868, 23: 50.679999999998486, 27: 52.849999999998055, 30: 53.77999999999787, 29: 54.22999999999778, 24: 54.499999999997726, 21: 55.03999999999762, 15: 58.159999999997, 17: 58.56999999999692, 8: 62.05999999999622, 14: 66.41999999999707, 4: 79.06000000000354, 10: 87.63000000000793, 19: 110.38000000001956, 31: 146.84000000001143, 28: 159.5199999999999, 18: 0.0}
    netman = NetworkManager(net=netwrk,
                            name='temp',
                            base_dir='D:/BigData/Charity/Cascades/')
    netman.save_to_file()

    result = Simulator.simulate_SI_decay_confirm_relicexp_hetero(
        underlying=netwrk,
        theta=true_theta,
        relic=true_relic,
        confirm=.0,
        decay=.0,
        infected={1},
        tmax=300,
        dt=0.01)
    result['outcome'][1] = .0
    #result['outcome'] = test_outcome #OVERWRITE TEST
    # result = Simulator.simulate_SI_relic(underlying=ntw, theta=true_theta, relic=true_relic,
    #                             infected={1}, tmax=300, dt=0.01)
    if (newlib):
        est.set_diffusion_data_newlib(netman.get_dos_filepath(),
                                      outcome=result['outcome'],
                                      observe_time=300,
                                      echo=False)
    else:
        est.set_diffusion_data(netman.get_dos_filepath(),
                               counters={},
                               outcome=result['outcome'])
    est_th_py = .0
    est_dc_py = .0
    est_th_c = .0
    est_dc_c = .0
    best_pest_py = 0
    best_pest_c = 0
    ml_pval = 0
    total_pest = 0
    th = 0.0
    thetas = np.arange(0.0001, 0.05, 0.0005)
    relics = np.arange(0.0001, 0.01, 0.0001)
    ps_py = np.zeros((len(thetas), len(relics)))
    ps_c = np.zeros((len(thetas), len(relics)))
    i = 0
    if (single_point):
        print(
            math.exp(
                Simulator.estimate_SI_relic_continuous(
                    underlying=ntw,
                    outcome=result['outcome'],
                    theta=true_theta,
                    relic=true_relic,
                    initials={1},
                    tmax=300,
                    echo=False)))
        if (newlib):
            print(
                math.exp(-est.loglikelyhood_SI_relic_newlib(
                    [true_theta, true_relic])))
        else:
            print(
                math.exp(
                    -est.loglikelyhood_SI_relic([true_theta, true_relic])))
    else:
        for th in thetas:
            dc = 0.0
            j = 0
            for dc in relics:
                pest_py = math.exp(
                    Simulator.estimate_SI_relic_continuous(
                        underlying=ntw,
                        outcome=result['outcome'],
                        theta=th,
                        relic=dc,
                        initials={1},
                        tmax=300))
                if (newlib):
                    pest_c = math.exp(
                        -est.loglikelyhood_SI_relic_newlib([th, dc]))
                else:
                    pest_c = math.exp(-est.loglikelyhood_SI_relic([th, dc]))
                #            print(str(th) + ' ' + str(pest))
                if pest_py > best_pest_py:
                    best_pest_py = pest_py
                    est_th_py = th
                    est_dc_py = dc
                if pest_c > best_pest_c:
                    best_pest_c = pest_c
                    est_th_c = th
                    est_dc_c = dc
                ps_py[i][j] = pest_py
                ps_c[i][j] = pest_c
                j += 1
            i += 1
        print('True theta: ' + str(true_theta))
        print('Max liklehood estimation Py: ' + str(est_th_py))
        print('Max liklehood estimation C: ' + str(est_th_c))
        print('True relic: ' + str(true_relic))
        print('Max liklehood estimation Py: ' + str(est_dc_py))
        print('Max liklehood estimation C: ' + str(est_dc_c))
        print('Likelyhood Py: ' + str(best_pest_py))
        print('Likelyhood C: ' + str(best_pest_c))

        relics, thetas = np.meshgrid(relics, thetas)

        plt.figure(figsize=(5, 5))
        plt.xlabel('$\\theta$ (virulence)')
        plt.ylabel('$\\rho$ (background)')
        plt.contourf(thetas, relics, ps_py, levels=15)
        plt.scatter([est_th_py], [est_dc_py], color='blue', marker='+')
        plt.scatter([true_theta], [true_relic], color='red', marker='o')
        plt.show()

        plt.figure(figsize=(5, 5))
        plt.xlabel('$\\theta$ (virulence)')
        plt.ylabel('$\\rho$ (background)')
        plt.contourf(thetas, relics, ps_c, levels=15)
        plt.scatter([est_th_c], [est_dc_c], color='blue', marker='+')
        plt.scatter([true_theta], [true_relic], color='red', marker='o')
        plt.show()
def test_SI_relic(ntw: Network,
                  true_theta: float,
                  true_relic: float,
                  continuous=False):
    dt = 1
    if continuous:
        dt = 0.01
    result = Simulator.simulate_SI_relic(underlying=ntw,
                                         theta=true_theta,
                                         relic=true_relic,
                                         infected={1},
                                         tmax=300,
                                         dt=dt)
    print(result)
    est_th = .0
    est_dc = .0
    best_pest = 0
    ml_pval = 0
    total_pest = 0
    th = 0.0
    thetas = np.arange(0.001, 0.11, 0.001)
    relics = np.arange(0.001, 0.11, 0.001)
    ps = np.zeros((len(thetas), len(relics)))
    i = 0
    for th in thetas:
        dc = 0.0
        j = 0
        for dc in relics:
            if continuous:
                pest = math.exp(
                    Simulator.estimate_SI_relic_continuous(
                        underlying=ntw,
                        outcome=result['outcome'],
                        theta=th,
                        relic=dc,
                        initials={1},
                        tmax=300))
            else:
                pest = Simulator.estimate_SI_relic(underlying=ntw,
                                                   outcome=result['outcome'],
                                                   theta=th,
                                                   relic=dc,
                                                   initials={1},
                                                   tmax=300,
                                                   dt=dt)


#            print(str(th) + ' ' + str(pest))
            if pest > best_pest:
                best_pest = pest
                est_th = th
                est_dc = dc
            ps[i][j] = pest
            total_pest += pest
            j += 1
        i += 1
    print('True theta: ' + str(true_theta))
    print('Max liklehood estimation: ' + str(est_th))
    print('True relic: ' + str(true_relic))
    print('Max liklehood estimation: ' + str(est_dc))
    print('Probability: ' + str(best_pest))

    relics, thetas = np.meshgrid(relics, thetas)

    # fig = plt.figure()
    # ax = Axes3D(fig)
    # ax.plot_surface(thetas, relics, ps, rstride=1, cstride=1, cmap=cm.viridis)
    # plt.show()

    plt.figure(figsize=(5, 5))
    plt.xlabel('$\\theta$ (virulence)')
    plt.ylabel('$\\rho$ (background)')
    plt.contourf(thetas, relics, ps, levels=15)
    plt.scatter([est_th], [est_dc], color='blue', marker='+')
    plt.scatter([true_theta], [true_relic], color='red', marker='o')
    plt.show()
def test_SI_halflife(ntw: Network,
                     true_theta: float,
                     true_halflife: float,
                     continuous=False):
    dt = 1
    if continuous:
        dt = 0.01
    result = Simulator.simulate_SI_halflife(underlying=ntw,
                                            theta=true_theta,
                                            halflife=true_halflife,
                                            infected={1},
                                            tmax=300,
                                            dt=dt)
    #result = Simulator.simulate_SI(underlying=ntw, theta=true_theta, infected={1}, tmax=300, dt=1)
    print(result)
    est_th = .0
    est_dc = .0
    best_pest = 0
    ml_pval = 0
    total_pest = 0
    th = 0.0
    thetas = np.arange(0.001, 1.001, 0.01)
    #thetas = np.array([true_theta])
    recovers = np.arange(1, 50, 0.5)
    #recovers = np.array([true_halflife])
    ps = np.zeros((len(thetas), len(recovers)))
    i = 0
    for th in thetas:
        dc = 0.0
        j = 0
        for dc in recovers:
            if continuous:
                pest = math.exp(
                    Simulator.estimate_SI_relic_halflife_continuous(
                        underlying=ntw,
                        outcome=result['outcome'],
                        theta=th,
                        halflife=dc,
                        relic=.0,
                        initials={1},
                        tmax=300))
            else:
                pest = Simulator.estimate_SI_halflife(
                    underlying=ntw,
                    outcome=result['outcome'],
                    theta=th,
                    halflife=dc,
                    initials={1},
                    tmax=300,
                    dt=dt)
#            print(str(th) + ' ' + str(pest))
            if pest > best_pest:
                best_pest = pest
                est_th = th
                est_dc = dc
            ps[i][j] = pest
            total_pest += pest
            j += 1
        i += 1


#    pval = de.estimate_pvalue_SI_decay(underlying=ntw, outcome=result['outcome'], theta=est_th, initials={1}, tmax=300, dt=1,
#                                 n_iterations=1000)
    print('True theta: ' + str(true_theta))
    print('Max liklehood estimation: ' + str(est_th))
    print('True halflife: ' + str(true_halflife))
    print('Max liklehood estimation: ' + str(est_dc))
    print('Probability: ' + str(best_pest))
    #    print('p-val: ' + str(pval))
    # psnorm = stat.normalize_series(ps)
    # confide = stat.confidential_from_p_series(psnorm, 0.95)
    # print('0.95 confidential interval: (' + str(thetas[confide[0]]) + ', ' + str(thetas[confide[1]]) + ')')

    decays, thetas = np.meshgrid(recovers, thetas)

    # fig = plt.figure()
    # ax = Axes3D(fig)
    # ax.plot_surface(thetas, decays, ps, rstride=1, cstride=1, cmap=cm.viridis)
    # plt.show()

    plt.figure(figsize=(5, 5))
    plt.xlabel('$\\theta$ (virulence)')
    plt.ylabel('$\\delta$ (halflife)')
    plt.contourf(thetas, decays, ps, levels=15)
    plt.scatter([est_th], [est_dc], color='blue', marker='+')
    plt.scatter([true_theta], [true_halflife], color='red', marker='o')
    plt.show()
def test_display_simulation_infections_ensemble(netwrk, theta, decay, relic,
                                                n_infections):
    netman = NetworkManager(net=netwrk,
                            name='temp',
                            base_dir='D:/BigData/Charity/Cascades/')
    netman.save_to_file()
    dt = 0.01
    tmax = 80

    outcomes = []
    initials = []
    for kk in range(n_infections):
        start_id = random.randint(1, 29)
        start_id = 1
        initials.append(start_id)
        result = Simulator.simulate_SI_decay_confirm_relicexp_hetero(
            underlying=netwrk,
            theta=theta,
            relic=relic,
            confirm=.0,
            decay=decay,
            infected={start_id},
            tmax=tmax,
            dt=dt,
            echo=False)
        result['outcome'][start_id] = .0
        outcomes.append(result['outcome'])
    est.set_diffusion_data_ensemble(netman.get_dos_filepath(),
                                    counters={},
                                    outcomes=outcomes,
                                    echo=True)

    # p1 = .0
    # for kk in range(n_infections):
    #     p = math.log(Simulator.estimate_SI_decay_confirm_relicexp_hetero(underlying=netwrk, theta=theta,
    #                                                                           relic=relic,
    #                                                                           confirm=.0, decay=decay,
    #                                                                           initials={initials[kk]},
    #                                                                           tmax=tmax, dt=dt, echo=True,
    #                                                                           outcome=outcomes[kk])
    #               /(dt**(len(outcomes[kk])-1)))
    #     p1 += p
    #     print(str(p) + '\n')
    #
    # p2 = -est.loglikelyhood_noconfirm_ensemble([theta, decay, relic], observe_time=tmax)
    # print(p1)
    # print(p2)
    #
    # return

    thetas = np.arange(0.0001, 0.05, 0.0005)
    relics = np.arange(0.0001, 0.05, 0.0005)
    ps = np.zeros((len(thetas), len(relics)))
    ps2 = np.zeros((len(thetas), len(relics)))
    best_pest = 1.0
    best_pest2 = 1.0
    # for kk in range(n_infections):
    #     best_pest += math.log(Simulator.estimate_SI_decay_confirm_relicexp_hetero(underlying=netwrk, theta=thetas[0],
    #                                                                 relic=relic,
    #                                                                 confirm=.0, decay=relics[0], initials={initials[kk]},
    #                                                                 tmax=tmax, dt=dt, echo=False,
    #                                                                 outcome=outcomes[kk])/(dt**(len(outcomes[kk])-1)))
    best_pest2 = -est.loglikelyhood_noconfirm_ensemble(
        [thetas[0], relics[0], relic], observe_time=tmax)
    est_th = 0
    est_dc = 0
    est_th2 = 0
    est_dc2 = 0
    i = 0
    for th in thetas:
        dc = 0.0
        j = 0
        for dc in relics:
            pest = .0
            # for kk in range(n_infections):
            #     pest += math.log(Simulator.estimate_SI_decay_confirm_relicexp_hetero(underlying=netwrk, theta=th,
            #                                                                 relic=relic,
            #                                                     confirm=.0, decay=dc, initials={initials[kk]},
            #                                                     tmax=tmax, dt=dt, echo=False,
            #                                                                 outcome=outcomes[kk])/(dt**(len(outcomes[kk])-1)))
            pest2 = -est.loglikelyhood_noconfirm_ensemble([th, dc, relic],
                                                          observe_time=tmax)
            print(
                str(th) + ' ' + str(dc) + ' ' + str(pest) + ' ' + str(pest2) +
                ' ' + str(abs(pest - pest2)))
            if pest > best_pest:
                best_pest = pest
                est_th = th
                est_dc = dc
            ps[i][j] = pest
            if pest2 > best_pest2:
                best_pest2 = pest2
                est_th2 = th
                est_dc2 = dc
            ps2[i][j] = pest2
            j += 1
        i += 1

    relics, thetas = np.meshgrid(relics, thetas)

    border_pest = best_pest - 300
    for i in range(len(thetas)):
        for j in range(len(relics)):
            if ps[i][j] >= border_pest:
                ps[i][j] = math.exp(ps[i][j] - border_pest)
            else:
                ps[i][j] = .0

    plt.subplot(1, 2, 1)
    #plt.figure(figsize=(5, 5))
    plt.xlabel('$\\theta$ (virulence)')
    plt.ylabel('$\\delta$ (decay)')
    plt.contourf(thetas, relics, ps, levels=15)
    plt.scatter([est_th], [est_dc], color='blue', marker='+')
    plt.scatter([theta], [decay], color='red', marker='o')
    #plt.show()

    border_pest2 = best_pest2 - 300
    for i in range(len(thetas)):
        for j in range(len(relics)):
            if ps2[i][j] >= border_pest2:
                ps2[i][j] = math.exp(ps2[i][j] - border_pest2)
            else:
                ps2[i][j] = .0

    plt.subplot(1, 2, 2)
    #plt.figure(figsize=(5, 5))
    plt.xlabel('$\\theta$ (virulence)')
    plt.ylabel('$\\delta$ (decay)')
    plt.contourf(thetas, relics, ps2, levels=15)
    plt.scatter([est_th2], [est_dc2], color='blue', marker='+')
    plt.scatter([theta], [decay], color='red', marker='o')
    plt.show()
def test_simulation_infections_ensemble_by_observe_time(
        netwrk, theta, decay, relic, n_infections, tmax, dt):
    netman = NetworkManager(net=netwrk,
                            name='temp',
                            base_dir='D:/BigData/Charity/Cascades/')
    netman.save_to_file()

    ths = []
    rels = []
    decs = []
    thnorms = []
    th = []
    rl = []
    dc = []
    outcomes = []
    for kk in range(n_infections):
        start_id = random.randint(1, 29)
        result = Simulator.simulate_SI_decay_confirm_relicexp_hetero(
            underlying=netwrk,
            theta=theta,
            relic=relic,
            confirm=.0,
            decay=decay,
            infected={start_id},
            tmax=tmax,
            dt=0.01)
        result['outcome'][start_id] = .0
        outcomes.append(result['outcome'])

    for i in range(40, tmax, dt):
        outcomes_trimmed = []
        for outcome in outcomes:
            new_outcome = {}
            for node_id, time in outcome.items():
                if time <= i:
                    new_outcome[node_id] = time
            outcomes_trimmed.append(new_outcome)
        print(outcomes_trimmed)

        est.set_diffusion_data_ensemble(netman.get_dos_filepath(),
                                        counters={},
                                        outcomes=outcomes_trimmed,
                                        echo=True)
        estimation = est.llmax_for_diffusion_noconfirm_ensemble(
            theta * math.exp((0.5 - random.random()) * 4),
            decay,
            relic * math.exp((0.5 - random.random()) * 4),
            disp=True,
            observe_time=i)
        print('Max: ' + str(estimation))
        print('Norm: ' + str(estimation['theta'] / estimation['relic']))
        ths.append(estimation['theta'])
        rels.append(estimation['relic'])
        decs.append(estimation['decay'])
        thnorms.append(estimation['theta'] / estimation['relic'])
        th.append(theta)
        rl.append(relic)
        dc.append(decay)
    # plt.plot(ths)
    # plt.plot(rels)
    # plt.plot(decs)
    plt.plot(ths, 'r')
    plt.plot(th, 'r--')
    plt.plot(rels, 'g')
    plt.plot(rl, 'g--')
    plt.plot(decs, 'b')
    plt.plot(dc, 'b--')
    plt.show()
def test_simulation_infections_ensemble_by_n_infections(
        netwrk, theta, decay, relic, n_infections, newlib=False):
    netman = NetworkManager(net=netwrk,
                            name='temp',
                            base_dir='D:/BigData/Charity/Cascades/')
    netman.save_to_file()

    ths = []
    rels = []
    decs = []
    thnorms = []
    th = []
    rl = []
    dc = []
    for i in range(n_infections):
        outcomes = []
        for kk in range(i):
            start_id = random.randint(1, 29)
            result = Simulator.simulate_SI_decay_confirm_relicexp_hetero(
                underlying=netwrk,
                theta=theta,
                relic=relic,
                confirm=.0,
                decay=decay,
                infected={start_id},
                tmax=300,
                dt=0.01)
            result['outcome'][start_id] = .0
            outcomes.append(result['outcome'])
        if newlib == True:
            observe_times = [600 for j in range(len(outcomes))]
            est.set_diffusion_data_ensemble_newlib(netman.get_dos_filepath(),
                                                   outcomes=outcomes,
                                                   observe_times=observe_times,
                                                   echo=True)
            estimation = est.llmax_for_diffusion_noconfirm(
                theta * math.exp((0.5 - random.random()) * 4),
                decay,
                relic * math.exp((0.5 - random.random()) * 4),
                disp=True,
                new_lib=True)
        else:
            est.set_diffusion_data_ensemble(netman.get_dos_filepath(),
                                            counters={},
                                            outcomes=outcomes,
                                            echo=True)
            estimation = est.llmax_for_diffusion_noconfirm_ensemble(
                theta * math.exp((0.5 - random.random()) * 4),
                decay,
                relic * math.exp((0.5 - random.random()) * 4),
                disp=True,
                observe_time=600)
        print('Max: ' + str(estimation))
        print('Norm: ' + str(estimation['theta'] / estimation['relic']))
        ths.append(estimation['theta'])
        rels.append(estimation['relic'])
        decs.append(estimation['decay'])
        thnorms.append(estimation['theta'] / estimation['relic'])
        th.append(theta)
        rl.append(relic)
        dc.append(decay)
    # plt.plot(ths)
    # plt.plot(rels)
    # plt.plot(decs)
    plt.plot(ths, 'r')
    plt.plot(th, 'r--')
    plt.plot(rels, 'g')
    plt.plot(rl, 'g--')
    plt.plot(decs, 'b')
    plt.plot(dc, 'b--')
    plt.show()
def test_simulation_dlldthetas(netwrk, theta, decay, relic):
    netman = NetworkManager(net=netwrk,
                            name='temp',
                            base_dir='D:/BigData/Charity/Cascades/')
    netman.save_to_file()
    targ_id = 2
    #    for i in range(100):
    thetas = {}
    for j in netman.network.nodes_ids:
        thetas[j] = theta * math.exp((0.5 - random.random()) * 4)
    thetas[targ_id] *= 10
    l = list(thetas.items())
    l.sort(key=lambda i: i[1])
    true_rank = 0
    i = 0
    for el in l:
        print(str(el[0]) + ' : ' + str(el[1]))
        i += 1
        if el[0] == targ_id:
            true_rank = i

    N = 10
    avg_dll_rank = .0
    avg_ll_rank = .0
    avg_dll_corr = .0
    avg_ll_corr = .0
    dll_rank_diff = .0
    ll_rank_diff = .0
    for kk in range(N):
        result = Simulator.simulate_SI_decay_confirm_relicexp_hetero(
            underlying=netwrk,
            theta=theta,
            relic=relic,
            confirm=.0,
            decay=decay,
            infected={1},
            tmax=300,
            dt=0.01,
            thetas=thetas)
        result['outcome'][1] = .0
        #print(result)
        est.set_diffusion_data(netman.get_dos_filepath(),
                               counters={},
                               outcome=result['outcome'],
                               echo=False)
        estimation = est.llmax_for_diffusion_noconfirm(theta,
                                                       decay,
                                                       relic,
                                                       disp=False)
        #print('Max: ' + str(estimation))
        dlldthetas = est.dlldthetas_noconfirm(estimation['theta'],
                                              estimation['decay'],
                                              estimation['relic'],
                                              result['outcome'].keys(), 300)

        outcome_sorted = list(result['outcome'].items())
        outcome_sorted.sort(key=lambda i: i[1])
        out_thetas = []
        llthetas_sorted = []
        llthetas = {}
        dlldthetas_sorted = []
        for node_id in result['outcome'].keys():
            out_thetas.append(thetas[node_id])
            dlldthetas_sorted.append(dlldthetas[node_id])
            llthetas[
                node_id] = est.llmax_for_diffusion_noconfirm_by_node_theta(
                    node_id,
                    estimation['theta'],
                    estimation['decay'],
                    estimation['relic'], (0.0001, 1.),
                    disp=False)
            llthetas_sorted.append(llthetas[node_id])

        avg_dll_corr += sp_stat.spearmanr(out_thetas, dlldthetas_sorted)[0]
        avg_ll_corr += sp_stat.spearmanr(out_thetas, llthetas_sorted)[0]

        print('true_rank: ' + str(true_rank))

        l = list(dlldthetas.items())
        l.sort(key=lambda i: i[1])
        for pp in range(len(l)):
            if l[pp][0] == targ_id:
                print('dll_rank: ' + str(pp))
                avg_dll_rank += pp
                dll_rank_diff += abs(pp - true_rank)
                break

        l = list(llthetas.items())
        l.sort(key=lambda i: i[1])
        for pp in range(len(l)):
            if l[pp][0] == targ_id:
                print('ll_rank: ' + str(pp) + '   est_theta: ' +
                      str(l[pp][1]) + '   theta: ' + str(estimation['theta']))
                avg_ll_rank += pp
                ll_rank_diff += abs(pp - true_rank)
                break

    print('avg_dll_rank: ' + str(avg_dll_rank / N))
    print('avg_dll_corr: ' + str(avg_dll_corr / N))
    print('avg_ll_rank: ' + str(avg_ll_rank / N))
    print('avg_ll_corr: ' + str(avg_ll_corr / N))
    print('true_rank: ' + str(true_rank))
    print('dll_rank_diff ' + str(dll_rank_diff / N))
    print('ll_rank_diff ' + str(ll_rank_diff / N))