示例#1
0
    def test_homo(self, gamma=3):
        n_trunc = 1000
        kq, kd = 15, 30
        lam = 0.2

        diffusion = Diffusion(self.cross_gallery_fc)
        inverse = diffusion.get_laplacian_inverse(n_trunc, kd)
        knn = KNN(self.cross_gallery_fc, method='cosine')
        sims, ids = knn.search(self.cross_query_fc, kq)
        sims[sims < 0] = 0
        sims /= np.sum(sims, axis=-1).reshape(-1, 1)
        sims = sims**gamma
        scores_qg = np.empty(
            (len(self.test_query_set), len(self.test_gallery_set)),
            dtype=np.float32)
        for i in range(len(self.test_query_set)):
            scores_qg[i] = (sims[i] @ inverse[ids[i]])

        diffusion = Diffusion(self.cross_query_fc)
        inverse = diffusion.get_laplacian_inverse(n_trunc, kd)
        knn = KNN(self.cross_query_fc, method='cosine')
        sims, ids = knn.search(self.cross_gallery_fc, kq)
        sims[sims < 0] = 0
        sims /= np.sum(sims, axis=-1).reshape(-1, 1)
        sims = sims**gamma
        scores_gq = np.empty(
            (len(self.test_gallery_set), len(self.test_query_set)),
            dtype=np.float32)
        for i in range(len(self.test_gallery_set)):
            scores_gq[i] = (sims[i] @ inverse[ids[i]])

        scores = lam * scores_qg + (1 - lam) * scores_gq.T
        self.evaluate(-scores)
    def test_middle(self):
        """
        Changing middle compartment, affects outer compartments by the same amount
        """
        # TODO: why does 'Compartment' fail the test (presumable an anion issue causing different steady-state cli)
        self.compBase = SimpleCompartment("c1", pkcc2=1e-8, z=-0.85)
        self.comp = self.compBase.copy("left")
        self.comp2 = self.comp.copy("right")

        # set diffusion value
        cli_D = 2.03
        cli_D *= 1e-7  # um2 to dm2 (D in dm2/ss)
        ki_D = 1.96
        ki_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        nai_D = 1.33
        nai_D *= 1e-7
        # create diffusion connection
        diffusion_object = Diffusion(self.comp, self.comp2, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})
        diffusion_object = Diffusion(self.comp2, self.compBase, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})

        self.sim.run(stop=100, dt=0.001, block_after=False)

        self.compBase.gx = 1e-8
        self.sim.run(continuefor=1, dt=1e-6, block_after=False)
        self.assertEqual(self.comp.cli, self.comp2.cli)

        self.compBase.gx = 0e-8
        self.sim.run(continuefor=1, dt=1e-6, block_after=False)
        self.assertEqual(self.comp.cli, self.comp2.cli)
示例#3
0
def search():
    st = time.time()
    n_query = len(queries)
    print("min..", np.min(queries))
    print("max..", np.max(queries))

    diffusion = Diffusion(np.vstack([queries, gallery]), args.cache_dir)
    print("diffusion shape",diffusion.features.shape)

    # offline type : scipy.sparse.csr.csr_matrix (희소행렬)
    # offline shape : (5118 ,5118) >> query와 gallery의 합
    offline = diffusion.get_offline_results(args.truncation_size, args.kd)
    # offline.data 안에 nan 값이 존재함 
    print("offline..", len(offline.data))

    print("offline features shape..",offline.shape)

    offline.data = np.nan_to_num(offline.data, copy=False)

    # 컬럼별로 확률화(SUM=1), feature=(1-a)(1-aS)의 역행렬
    features = preprocessing.normalize(offline, norm="l2", axis=1)
    print("features..",features.shape)

    scores = features[:n_query] @ features[n_query:].T
    np.save("pirsData/scores/"+ args.cate +"_scores.npy", -scores.todense())

    print("1> features[:n_query].shape :", features[:n_query].shape)
    print("2> features[n_query:].shape :", features[n_query:].shape)
    print("3> scores.shape :", scores.shape)
    # scores.shape : (55, 5063) = (쿼리, 갤러리) = (row, col)

    ranks = np.argsort(-scores.todense())
    #np.save("pirsData/ranks/"+ args.cate +"_ranks.npy", ranks)
    print("ranks[0]...\n", ranks[:10,:10])
    print("time check...>>>", args.cate,">>>", time.time()-st, ">>>", len(queries))
示例#4
0
def search():
    n_query = len(queries)
    diffusion = Diffusion(np.vstack([queries, gallery]), args.cache_dir)
    offline = diffusion.get_offline_results(args.truncation_size, args.kd)
    features = preprocessing.normalize(offline, norm="l2", axis=1)
    scores = features[:n_query] @ features[n_query:].T
    ranks = np.argsort(-scores.todense())
    evaluate(ranks)
示例#5
0
def test_sis_model():
    params = {
        'model': 'SIS',
        'b': 0.00208,
        'd': 0.01,
        'c': 1,
        'runs': 10,
        'steps': 5000,

        'diffusion': 'max',
        'method': 'add_edge_random',
        'k': 15,

        'seed': 1,
        'plot_transition': False,
        'gif_animation': False
    }

    graph = karate()

    ds = Diffusion(graph, params)
    increased_diffusion = ds.run_simulation()

    params['diffusion'] = None
    params['method'] = None
    params['k'] = 0

    ds = Diffusion(graph, params)
    baseline_diffusion = ds.run_simulation()

    params['diffusion'] = 'min'
    params['method'] = 'ns_node'
    params['k'] = 4

    ds = Diffusion(graph, params)
    decreased_diffusion = ds.run_simulation()

    assert sum(decreased_diffusion) < sum(baseline_diffusion) < sum(increased_diffusion)
    def test_mols(self):
        self.compBase = SimpleCompartment("c1", pkcc2=1e-8, z=-0.85,
                                          cli=0.015292947537423218,
                                          ki=0.023836660428807395,
                                          nai=0.1135388427892471)

        self.comp = self.compBase.copy("left")
        self.comp2 = self.comp.copy("right")
        self.compSingle = SimpleCompartment("cs", pkcc2=1e-8, z=-0.85,
                                            cli=0.015292947537423218,
                                            ki=0.023836660428807395,
                                            nai=0.1135388427892471,
                                            length=3 * default_length)
        # set diffusion value
        cli_D = 2.03
        cli_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        ki_D = 1.96
        ki_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        nai_D = 1.33
        nai_D *= 1e-7
        # create diffusion connection
        diffusion_object = Diffusion(self.comp, self.compBase, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})
        diffusion_object = Diffusion(self.comp2, self.compBase, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})

        self.sim.run(stop=100, dt=0.001, block_after=False)

        self.compBase.gx = 1e-8
        self.compSingle.gx = 1e-8
        self.sim.run(continuefor=1, dt=1e-6, block_after=False)

        xmol_3 = self.compBase.mols(self.compBase.xi) + self.comp.mols(self.comp.xi) + self.comp2.mols(self.comp2.xi)
        xmol_single = self.compSingle.mols(self.compSingle.xi)
        self.assertAlmostEqual(xmol_3, xmol_single)

        cmol_3 = self.compBase.mols(self.compBase.cli) + self.comp.mols(self.comp.cli) + self.comp2.mols(self.comp2.cli)
        cmol_single = self.compSingle.mols(self.compSingle.cli)
        self.assertAlmostEqual(cmol_3, cmol_single)
示例#7
0
    def __init__(self, options = None):
        super().__init__(options)
        self.layout.split(
            Layout(name='upper'),
            Layout(name='lower')
        )

        self.layout["lower"].update(self.menu)


        self.maps = dict()
        self.maps["diffusion"] = Diffusion(80,30, 300)
        self.maps["life"] = Life(140,30,5,80)

        self.layout["upper"].size = 35
        self.populate()
示例#8
0
    def __init__(self, options=None):
        super().__init__(options)

        #Upper is interactive map preview
        #Lower is menu options
        self.layout.split(Layout(name='upper'), Layout(name='lower'))

        self.sleepTime = .5

        self.maps = dict()
        self.maps["diffusion"] = Diffusion(80, 30, 300)
        self.maps["life"] = Life(140, 30, 5, 80)

        self.layout["upper"].size = 35
        self.layout["lower"].update(self.menu)
        self.populate()
    def test_one_is_two(self):
        """
        Changing to the same values of all (2) compartments is the same as changing as if it were compartment
        """
        self.compBase = Compartment("c1", length=10e-5, pkcc2=0, z=-0.85,
                                    cli=0.015292947537423218,
                                    ki=0.023836660428807395,
                                    nai=0.1135388427892471)

        self.comp = Compartment("c1", length=5e-5, pkcc2=0, z=-0.85,
                                cli=0.015292947537423218,
                                ki=0.023836660428807395,
                                nai=0.1135388427892471)
        self.comp2 = self.comp.copy("c2")
        # set diffusion value
        cli_D = 2.03
        cli_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        ki_D = 1.96
        ki_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        nai_D = 1.33
        nai_D *= 1e-7
        # create diffusion connection
        diffusion_object = Diffusion(self.comp, self.comp2, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})

        self.assertEqual(self.compBase.cli, self.comp.cli)

        self.sim.run(stop=100, dt=0.001, block_after=False)

        self.assertEqual(self.compBase.cli, self.comp.cli)
        self.assertEqual(self.comp.cli, self.comp2.cli)

        self.compBase.gx = self.comp.gx = self.comp2.gx = 1e-8

        self.sim.run(continuefor=1, dt=1e-6, block_after=False)

        self.assertEqual(self.compBase.cli, self.comp.cli)
        self.assertEqual(self.comp.cli, self.comp2.cli)
示例#10
0
def search_old(gamma=3):
    diffusion = Diffusion(gallery, args.cache_dir)
    offline = diffusion.get_offline_results(args.truncation_size, args.kd)

    time0 = time.time()
    print('[search] 1) k-NN search')
    sims, ids = diffusion.knn.search(queries, args.kq)
    sims = sims**gamma
    qr_num = ids.shape[0]

    print('[search] 2) linear combination')
    all_scores = np.empty((qr_num, args.truncation_size), dtype=np.float32)
    all_ranks = np.empty((qr_num, args.truncation_size), dtype=np.int)
    for i in tqdm(range(qr_num), desc='[search] query'):
        scores = sims[i] @ offline[ids[i]]
        parts = np.argpartition(-scores,
                                args.truncation_size)[:args.truncation_size]
        ranks = np.argsort(-scores[parts])
        all_scores[i] = scores[parts][ranks]
        all_ranks[i] = parts[ranks]
    print('[search] search costs {:.2f}s'.format(time.time() - time0))

    # 3) evaluation
    evaluate(all_ranks)
示例#11
0
def run_epidemic_experiment(params):
    graph = as_733().copy()
    results = defaultdict(list)

    b_list = np.arange(0, 0.005, 0.001)  # transmission probability
    for idx, b in enumerate(b_list):

        params['b'] = b

        if idx == 1:
            params['plot_transition'] = True
            params['gif_animation'] = True
            params['gif_snaps'] = True
        else:
            params['plot_transition'] = False
            params['gif_animation'] = False
            params['gif_snaps'] = False

        ds = Diffusion(graph, **params)

        result = ds.run_simulation()
        results[ds.get_effective_strength()] = result

    plot_results(graph, params, results)
示例#12
0
                        required=True,
                        help="""
                        Path to gallery features
                        """)
    parser.add_argument('--ground_truth_path',
                        type=str,
                        help="""
                        Path to ground-truth
                        """)
    parser.add_argument('-n',
                        '--truncation_size',
                        type=int,
                        default=1000,
                        help="""
                        Number of images in the truncated gallery
                        """)
    args = parser.parse_args()
    args.kq, args.kd = 10, 50
    return args


if __name__ == "__main__":
    args = parse_args()
    if not os.path.isdir(args.cache_dir):
        os.makedirs(args.cache_dir)
    if args.ground_truth_path:
        evaluator = OxfordParisEvaluator(args.ground_truth_path)
    dataset = Dataset(args.query_path, args.gallery_path)
    diffusion = Diffusion(dataset.gallery, args.cache_dir)
    search(args)
def main(cli_D=2.03,
         new_gx=0e-8,
         anion_flux=False,
         default_xz=-0.85,
         jkccup=1e-12,
         nrcomps=2,
         dz=1e-7,
         textra=100,
         say='',
         stretch=False):
    """
    cli_D # um2/s
    :return: sim, gui: it is useful to return these objects for access after simulation
    """
    print("main")
    sim = Simulator().get_instance()
    gui = sim.gui()
    dt = 0.001  # s

    length = 10e-5

    comp = Compartment("reference",
                       z=-0.85,
                       cli=0.0052,
                       ki=0.0123,
                       nai=0.014,
                       length=length,
                       radius=default_radius_short,
                       stretch_w=stretch)

    # copies left
    compl = comp.copy("dendrite left")

    # copies right
    compr = []
    compr.append(comp.copy("dendrite right " + str(1)))
    for i in range(nrcomps):
        compr.append(comp.copy("dendrite right " + str(i + 2)))

    # find steady-state values of ions
    sim.run(stop=100,
            dt=0.001,
            plot_update_interval=50,
            data_collect_interval=5,
            block_after=False)

    # set diffusion value
    cli_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    ki_D = 1.96
    ki_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    nai_D = 1.33
    nai_D *= 1e-7
    diffusion_object = []

    # connect with Diffusion
    diffusion_object.append(
        Diffusion(compl, comp, ions={
            'cli': cli_D,
            'ki': ki_D,
            'nai': nai_D
        }))
    diffusion_object.append(
        Diffusion(comp,
                  compr[0],
                  ions={
                      'cli': cli_D,
                      'ki': ki_D,
                      'nai': nai_D
                  }))
    for i in range(nrcomps):
        diffusion_object.append(
            Diffusion(compr[i],
                      compr[i + 1],
                      ions={
                          'cli': cli_D,
                          'ki': ki_D,
                          'nai': nai_D
                      }))

    # heatmap incorporating compartment heights
    sc = 1e7
    htplot = Colormap("cmap", 0, compr)
    totalht, initvals = htplot.heatmap(compl,
                                       comp,
                                       compr,
                                       sc,
                                       0,
                                       all=1,
                                       init_vals=None)
    htplot.heatmap(compl, comp, compr, sc, totalht, all=1, init_vals=initvals)

    voltage_reversal_graph_comp = gui.add_graph() \
        .add_ion_conc(comp, "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(comp, "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(comp, line_style='k', y_units_scale=1000, y_plot_units='mV')

    voltage_reversal_graph_compr = gui.add_graph() \
        .add_ion_conc(compr[-1], "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(compr[-1], "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(compr[-1], line_style='k', y_units_scale=1000, y_plot_units='mV')

    voltage_reversal_graph_compl = gui.add_graph() \
        .add_ion_conc(compl, "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(compl, "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(compl, line_style='k', y_units_scale=1000, y_plot_units='mV')

    voltage_reversal_graph_compr1 = gui.add_graph() \
        .add_ion_conc(compr[0], "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(compr[0], "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(compr[0], line_style='k', y_units_scale=1000, y_plot_units='mV')

    # run simulation with diffusion
    sim.run(continuefor=10,
            dt=dt,
            plot_update_interval=5,
            data_collect_interval=1)
    print(datetime.datetime.now())
    print_concentrations(
        [comp, compl, compr[-1]],
        title="Ion concentrations given diffusion between compartments")

    htplot.heatmap(compl, comp, compr, sc, totalht, all=1, init_vals=initvals)

    # (optionally) change anion conductance
    prev_comp_gx = comp.gx
    comp.gx = new_gx
    comp.dz = dz

    voltage_reversal_graph_comp.save(say + 'reference.eps')

    if dz != 0:
        z_graph = gui.add_graph() \
            .add_ion_conc(comp, "z", line_style='m')

    if comp.gx > 0:
        x_graph = gui.add_graph() \
            .add_ion_conc(comp, "absox", line_style='m') \
            .add_ion_conc(compl, "absox", line_style=':m') \
            .add_ion_conc(compr[0], "absox", line_style='m--')

    # (optionally) change anion flux
    if anion_flux:
        comp.xz = default_xz
        comp.xmz = (comp.z * comp.xi - comp.xz * comp.xi_temp) / comp.xm
        print('Anion flux with fixed anions having net charge', comp.xmz,
              'while a proportion of', (1 - comp.ratio),
              'of all impermeants are temporarily mobile anions of charge',
              comp.xz)

    z_graph = gui.add_graph() \
        .add_ion_conc(comp, "z", line_style='m')

    # (optionally) change kcc2
    prev_comp_pkcc2 = comp.pkcc2
    if jkccup is not None:
        comp.jkccup = jkccup
        g_graph = gui.add_graph() \
            .add_ion_conc(comp, "pkcc2", line_style='k')

    vol_graph = gui.add_graph() \
        .add_ion_conc(comp, "w", line_style='b') \
        .add_ion_conc(compl, "w", line_style=':b') \
        .add_ion_conc(compr[0], "w", line_style='b--')

    sim.run(continuefor=textra,
            dt=dt * 0.001,
            plot_update_interval=textra / 32,
            data_collect_interval=textra / 32)
    print(datetime.datetime.now())
    print_concentrations(
        [comp, compl, compr[-1]],
        title="Ion concentrations during event from the dendritic compartment")
    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_halfway_df.eps', say + 'all_halfway_ecl.eps',
                       say + 'all_halfway_vm.eps'
                   ])

    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 16)
    print(datetime.datetime.now())
    print_concentrations(
        [comp, compl, compr[-1]],
        title=
        "Ion concentrations immediately after event from the dendritic compartment"
    )
    # heatmap incorporating compartment heights
    htplot.heatmap(compl, comp, compr, sc, totalht, all=1, init_vals=initvals)

    comp.gx = prev_comp_gx
    comp.jkccup = 0
    comp.dz = 0

    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 1,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at almost steady state")

    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 1,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 2,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 2,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 2,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    return sim, gui
示例#14
0
def _infer(model, root_path, test_loader=None, local_val=False):
    """
    모델과 데이터가 주어졌을 때, 다음과 같은 데이터 구조를 반환하는 함수를 만들어야 합니다.

    [ [ query_image_id_1, predicted_database_image_id_1 ],
      [ query_image_id_2, predicted_database_image_id_2 ],
      ...
      [ query_image_id_N, predicted_database_image_id_N ] ]

    README 설명에서처럼 predicted_database_image_id_n 은 query_image_id_n 에 대해
    평가셋 이미지 1,...,n-1,n+1,...,N 를 데이터베이스로 간주했을 때에 가장 쿼리와 같은 카테고리를
    가질 것으로 예측하는 이미지입니다. 이미지 아이디는 test_loader 에서 extract 되는 첫번째
    인자인 data_id 를 사용합니다.

    Args:
      model: 이미지를 인풋으로 받아서 feature vector를 반환하는 모델
      root_path: 데이터가 저장된 위치
      test_loader: 사용되지 않음
      local_val: 사용되지 않음

    Returns:
      top1_reference_ids: 위에서 설명한 list 데이터 구조
    """
    if test_loader is None:
        test_loader = test_data_loader(
            root=os.path.join(root_path, 'test_data'))

    # TODO 모델의 아웃풋을 적당히 가공하고 연산하여 각 query에 대해 매치가 되는 데이터베이스
    # TODO 이미지의 ID를 찾는 모듈을 구현 (현재 구현은 베이스라인 - L2 정규화 및 내적으로 가장
    # TODO 비슷한 이미지 조회).
    feats = None
    data_ids = None
    s_t = time.time()
    for idx, data_package in enumerate(test_loader):
        if local_val:
            data_id, image, _ = data_package
        else:
            data_id, image = data_package
        image = image.cuda()
        feat = model(image, extract=True)
        feat = feat.detach().cpu().numpy()
        feat = feat / np.linalg.norm(feat, axis=1)[:, np.newaxis]
        if feats is None:
            feats = feat
        else:
            feats = np.append(feats, feat, axis=0)
        if data_ids is None:
            data_ids = data_id
        else:
            data_ids = np.append(data_ids, data_id, axis=0)

        if time.time() - s_t > 10:
            print('Infer batch {}/{}.'.format(idx + 1, len(test_loader)))

    diffusion = Diffusion(feats, cache_dir='./cache')
    offline = diffusion.get_offline_results(n_trunc=1000, kd=50)
    features = preprocessing.normalize(offline, norm='l2', axis=1)

    score_matrix = features @ features.T
    np.fill_diagonal(score_matrix, -np.inf)
    top1_reference_indices = np.argmax(score_matrix, axis=1)
    top1_reference_ids = [[
        data_ids[idx], data_ids[top1_reference_indices[idx]]
    ] for idx in range(len(data_ids))]

    return top1_reference_ids
示例#15
0
    def diffusion(
        self,
        slice_time,
        synergy,
        states=None,
        time_stamps=None,
        nb_steps=1,
        infected_node=1,
        cooperator_ratio=0.0,
        strategies=None,
        payoffs=None
        ):

        from diffusion import Diffusion
        from pgg import PublicGoodGames
        
        G = self._slices[int(slice_time)]['graph'].copy()

        if states == None or time_stamps == None:
            # Initialize the Public Good Game 
            PGG = PublicGoodGames(G=G,
                    cooperator_ratio=cooperator_ratio, nb_simulation_step=nb_steps, synergy=synergy)

            # Get the startegies list
            strategies = PGG.get_strategies()

            # Initialize the disffusion process with the startegies list
            D = Diffusion(G=G, nb_simulation_step=nb_steps,
                          time_step=self._time_step,
                          initial_time_stamp=slice_time,
                          strategies=strategies)
            
            # Run the diffusion Process
            D.run_steps(infected_node)

            # Run the Game  
            PGG.run_game()

            # Return the strategies list, the payoffs list, the timestamps list and the states list 
            return (D.get_states(), D.get_time_stamps(), PGG.get_strategies(), PGG.get_payoffs())
        elif strategies != None and payoffs != None:

            D = Diffusion(G=G, nb_simulation_step=nb_steps,
                          time_step=self._time_step,
                          initial_time_stamp=slice_time,
                          strategies=strategies)

            PGG = PublicGoodGames(G=G, nb_simulation_step=nb_steps, synergy=synergy)
            
            PGG.set_strategies(strategies)
            PGG.set_payoffs(payoffs)

            D.set_nodes_states(states)
            D.set_node_time_stamps(time_stamps)

            # Run the diffusion Process
            D.run_steps(infected_node)
            # Run the Game  
            PGG.run_game()
            # Return the strategies list, the payoffs list, the timestamps list and the states list 
            return (D.get_states(), D.get_time_stamps(), PGG.get_strategies(), PGG.get_payoffs())
def grow(nr=3, textra=10):
    print("growing via anions")
    sim = Simulator().get_instance()
    gui = sim.gui()
    dt = 0.001  # s

    comp = []
    comp.append(
        Compartment("initial growth cone",
                    z=-0.85,
                    cli=0.00433925284075134,
                    ki=0.1109567493822927,
                    nai=0.0255226350779378,
                    length=5e-5,
                    radius=default_radius_short))

    # steady state
    sim.run(stop=100,
            dt=0.001,
            plot_update_interval=500,
            data_collect_interval=5,
            block_after=False)

    # set diffusion value
    cli_D = 2.03
    cli_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    ki_D = 1.96
    ki_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    nai_D = 1.33
    nai_D *= 1e-7

    #another compartment
    comp.append(comp[0].copy("compartment 1"))
    comp[1].L = 10e-5
    comp[1].w = np.pi * comp[1].r**2 * comp[1].L
    diffusion_object = [
        Diffusion(comp[0],
                  comp[1],
                  ions={
                      'cli': cli_D,
                      'ki': ki_D,
                      'nai': nai_D
                  })
    ]

    # heatmap incorporating compartment heights
    sc = 1e5
    htplot = Colormap("dendrite", comp[0].w + comp[1].w, comp)
    totalht, init_vals = htplot.smallheatmap(comp,
                                             sc,
                                             int(htplot.totalh * sc),
                                             all=0,
                                             init_val=None)

    # plot
    voltage_reversal_graph_comp = gui.add_graph() \
        .add_ion_conc(comp[0], "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(comp[0], "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(comp[0], line_style='k', y_units_scale=1000, y_plot_units='mV')

    volume_graph = gui.add_graph()
    volume_graph.add_var(volume_graph.time,
                         "time",
                         htplot,
                         "comp0w",
                         line_style='k')
    volume_graph.add_var(volume_graph.time,
                         "time",
                         htplot,
                         "totalh",
                         line_style='b')

    sim.run(continuefor=1,
            dt=dt * 0.001,
            plot_update_interval=0.5,
            data_collect_interval=textra / 16)

    # growth
    for i in range(nr):
        htplot.smallheatmap(comp,
                            sc,
                            totalht,
                            all=0,
                            init_val=init_vals,
                            name='graphs/grow_done' + str(i) + '.eps')
        comp[0].gx = 1

        # stop at certain length
        while comp[0].L < 15e-5:
            print("Fluxing compartment's length: " + str(comp[0].L))
            sim.run(continuefor=0.5,
                    dt=dt * 0.001,
                    plot_update_interval=0.25,
                    data_collect_interval=textra / 16)
            if 9.9e-5 < comp[0].L < 10.3e-5:
                htplot.smallheatmap(comp,
                                    sc,
                                    totalht,
                                    all=0,
                                    init_val=init_vals,
                                    name='graphs/grow_interim' + str(i) +
                                    '.eps')
        comp[0].gx = 0
        print_concentrations(comp, str(i))

        # split compartments
        comp.insert(0, comp[0].copy("compartment " + str(i)))
        comp[1].L -= 5e-5
        comp[0].L = 5e-5
        comp[0].w = np.pi * comp[0].r**2 * comp[0].L
        comp[1].w = np.pi * comp[1].r**2 * comp[1].L

        print_concentrations(comp, str(i))

        # update total height
        htplot.comp = comp

        # update diffusion
        diffusion_object.append(
            Diffusion(comp[0],
                      comp[1],
                      ions={
                          'cli': cli_D,
                          'ki': ki_D,
                          'nai': nai_D
                      }))

        for a in comp:
            print(a.name)

        for j in diffusion_object:
            print(j.name)

        sim.run(continuefor=10,
                dt=dt * 0.001,
                plot_update_interval=5,
                data_collect_interval=textra / 16)

    htplot.smallheatmap(comp,
                        sc,
                        totalht,
                        all=1,
                        init_val=init_vals,
                        name='graphs/grow_end.eps')
    sim.run(continuefor=4,
            dt=dt * 0.001,
            plot_update_interval=2,
            data_collect_interval=0.5)
    htplot.smallheatmap(comp,
                        sc,
                        totalht,
                        all=1,
                        init_val=init_vals,
                        name='graphs/grow_end.eps')

    return sim, gui
示例#17
0
def main():

	arg_parser = argparse.ArgumentParser()
	arg_parser.add_argument('train_images_path')
	arg_parser.add_argument('test_images_path')
	arg_parser.add_argument('predictions_path')
	args = arg_parser.parse_args()

	imsize=480
	train_path=args.train_images_path
	test_path=args.test_images_path
	outfile=args.predictions_path
	##read data##
	data_list=os.listdir(ipath)
	train_images = get_imlist(train_path)
	test_images = get_imlist(test_path)
	##RAMAC##
	RAMAC = extract_feature(train_images, 'resnet101', imsize)
	RAMAC_test = extract_feature(test_images, 'resnet101', imsize)
	##UEL##
	normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
	transform_train = transforms.Compose([
        transforms.ToPILImage(),
        transforms.RandomCrop(size=224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        normalize,
    ])
	transform_test = transforms.Compose([
        transforms.ToPILImage(),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        normalize,
    ])
	net = resnet101(pretrained=True,low_dim=128)
	model_path = './model/UEL.t'#After training UEL
	net.load_state_dict(torch.load())
	
	imset = DataLoader(path = train_path, transform=transform_test)
	train_loader = torch.utils.data.DataLoader(imset, batch_size=32, shuffle=False, num_workers=0)
	UEL = obtainf(net, train_loader)
	
	imset = DataLoader(path = test_path, transform=transform_test)
	test_loader = torch.utils.data.DataLoader(imset, batch_size=32, shuffle=False, num_workers=0)
	UEL_test = obtainf(net, test_loader)
	##GEM##
	image_size=1024
	multiscale='[1, 2**(1/2), 1/2**(1/2)]'
	state = torch.load('./model/retrievalSfM120k-vgg16-gem-b4dcdc6.pth')
	
	net_params = {}
	net_params['architecture'] = state['meta']['architecture']
	net_params['pooling'] = state['meta']['pooling']
	net_params['local_whitening'] = state['meta'].get('local_whitening', False)
	net_params['regional'] = state['meta'].get('regional', False)
	net_params['whitening'] = state['meta'].get('whitening', False)
	net_params['mean'] = state['meta']['mean']
	net_params['std'] = state['meta']['std']
	net_params['pretrained'] = False
	# load network
	net = init_network(net_params)
	net.load_state_dict(state['state_dict'])
        
	# if whitening is precomputed
	if 'Lw' in state['meta']:
		net.meta['Lw'] = state['meta']['Lw']
	ms = list(eval(multiscale))
	msp = net.pool.p.item()
	
	net.cuda()
	net.eval()
	# set up the transform
	normalize = transforms.Normalize(
		mean=net.meta['mean'],
		std=net.meta['std']
    )
	transform = transforms.Compose([
        transforms.ToTensor(),
        normalize
    ])
	
	GEM = extract_vectors(net,train_images , 480, transform, ms=ms, msp=msp).numpy().T
	GEM_test = extract_vectors(net,test_images , 480, transform, ms=ms, msp=msp).numpy().T
	##Retrieval##
	feats=np.concatenate((RAMAC,UEL,GEM),axis=1).astype('float32')
	query_feat=np.concatenate((RAMAC_test, UEL_test,GEM_test),axis=1).astype('float32')
	##diffusion##
	kq, kd = 7, 50
	gamma=80
	diffusion = Diffusion(feats, '/')
	offline = diffusion.get_offline_results(1024, kd)
	print('[search] 1) k-NN search')
	sims, ids = diffusion.knn.search(query_feat, kq)
	sims = sims ** gamma
	qr_num = ids.shape[0]
	print('[search] 2) linear combination')
	all_scores = np.empty((qr_num, 7), dtype=np.float32)
	all_ranks = np.empty((qr_num, 7), dtype=np.int)
	for i in range(qr_num):
		scores = sims[i] @ offline[ids[i]]
		parts = np.argpartition(-scores, 7)[:7]
		ranks = np.argsort(-scores[parts])
		all_scores[i] = scores[parts][ranks]
		all_ranks[i] = parts[ranks]
	I = all_ranks
	##output##
	out=pd.DataFrame(list(map(lambda x: x.split('/')[-1].split('.jpg')[0],timages)))
	out['1']=pd.DataFrame(I)[0].map(lambda x:data_list[x].split('.')[0] )
	out['2']=pd.DataFrame(I)[1].map(lambda x:data_list[x].split('.')[0] )
	out['3']=pd.DataFrame(I)[2].map(lambda x:data_list[x].split('.')[0] )
	out['4']=pd.DataFrame(I)[3].map(lambda x:data_list[x].split('.')[0] )
	out['5']=pd.DataFrame(I)[4].map(lambda x:data_list[x].split('.')[0] )
	out['6']=pd.DataFrame(I)[5].map(lambda x:data_list[x].split('.')[0] )
	out['7']=pd.DataFrame(I)[6].map(lambda x:data_list[x].split('.')[0] )
	out.to_csv(outfile,index=None,header=None)
 def test_diffusion_compartments(self, **kwargs):
     self.d = Diffusion(self.comp, self.comp2, self.ions)
     self.run_diffusion(300, False, **kwargs)