Exemplo n.º 1
0
    def plot_connection_histogram(self, gid, conn_type):

        self.load_connection_list(conn_type)
        targets = utils.get_targets(self.connection_lists[conn_type], gid)
        tgt_ids, tgt_weights, tgt_delays = targets[:, 1], targets[:, 2], targets[:, 3]

        sources = utils.get_sources(self.connection_lists[conn_type], gid)
        src_ids, src_weights, src_delays = sources[:, 1], sources[:, 2], sources[:, 3]
        
        fig = pylab.figure(figsize=(14, 10))
#        ax1 = fig.add_subplot(1, 1, 1) # set blank? the set title
        pylab.subplots_adjust(hspace=.35, wspace=.25)
        ax1 = fig.add_subplot(2, 2, 1)
        ax2 = fig.add_subplot(2, 2, 2)
        ax3 = fig.add_subplot(2, 2, 3)
        ax4 = fig.add_subplot(2, 2, 4)
        

        ax1.set_title('$\sigma^w_{X} = %.2f \sigma^w_{V}=%.2f$' % (self.params['w_sigma_x'], self.params['w_sigma_v']))
        tgt_weights_sorted = tgt_weights.copy()
        tgt_weights_sorted.sort()
        ax1.bar(range(len(tgt_ids)), tgt_weights_sorted, width=1)
        ax1.set_ylabel('Outgoing weights [uS]')
        ax1.set_xlabel('sorted targets')
        ax1.set_xlim((0, len(tgt_ids)))

        n_weight_bins = 20
        count, bins = np.histogram(tgt_weights, bins=n_weight_bins)
        ax2.bar(bins[:-1], count, width=bins[1] - bins[0])
        ax2.set_xlabel('Outgoing weight [uS]')
        ax2.set_ylabel('#')


        src_weights_sorted = src_weights.copy()
        src_weights_sorted.sort()
        ax3.bar(range(len(src_ids)), src_weights_sorted, width=1)
        ax3.set_ylabel('Incoming weights [uS]')
        ax3.set_xlabel('sorted sources')
        ax3.set_xlim((0, len(src_ids)))

        n_weight_bins = 20
        count, bins = np.histogram(src_weights, bins=n_weight_bins)
        ax4.bar(bins[:-1], count, width=bins[1] - bins[0])
        ax4.set_xlabel('Incoming weight [uS]')
        ax4.set_ylabel('#')

        output_fn = self.params['figures_folder'] + 'connection_histogram_wsigmaxv_%.2f_%.2f_%d.png' % (self.params['w_sigma_x'], self.params['w_sigma_v'], gid)
        print 'Saving fig to:', output_fn
        pylab.savefig(output_fn, dpi=200)
Exemplo n.º 2
0
def main():
    graph = rdflib.Graph()
    graph.parse(source="policies/assets.json", format="json-ld")

    for target in utils.get_targets(graph):
        utils.add_iri_hierarchy_to_graph(graph, target,
                                         predicate=ODRL.partOf, reverse=True)

    draw_graph(graph, ODRL.partOf, reverse=True)
    plt.show()

    graph = rdflib.Graph()
    graph.parse(location=JSON_LD["ODRL"], format="json-ld")

    draw_graph(graph, ODRL.includedIn, reverse=True)
    plt.show()
Exemplo n.º 3
0
    def find_exc_gid_to_plot(self):

        if os.path.exists(self.params['gids_to_record_fn']):
            good_gids = np.loadtxt(self.params['gids_to_record_fn'], dtype='int')
            idx = self.tp_exc[good_gids, 0].argsort()
            gids_to_check = good_gids[idx]
#            print 'debug x_pos', self.tp_exc[idx, 0]

            conn_list_ei = np.loadtxt(self.params['merged_conn_list_ei'])

            for gid in gids_to_check:
                inh_targets = utils.get_targets(conn_list_ei, gid)
                print 'gid %d at xpos %.2f has %d inh targets' % (gid, self.tp_exc[gid, 0], len(inh_targets))
                if len(inh_targets) > 0:
                    return gid
                 

        else: # choose any cell as source 
            gid = int(.5 * self.params['n_exc'])

        return gid
                  mechanisms=mechanisms,
                  magnitudes=magnitudes,
                  stf=stf)


    # setup stations/targets:
    #stats = load_stations(webnet+'/meta/stations.pf')
    stats = load_stations('stations.pf')
    #stats = []
    # Scrutinize the swarm using matplotlib

    noise = Noise(files='/media/usb/webnet/mseed/2008')

    # convert loaded stations to targets (see function at the top).
    #targets = guess_targets_from_stations(stats)
    targets = get_targets(stations, noise.data_pile, store_id=store_id)
    Visualizer(swarm, stats)

    # Processing that data will return a pyrocko.gf.seismosizer.Reponse object.
    response = engine.process(sources=swarm.get_sources(),
                              targets=targets)

    # Save the events
    dump_events(swarm.get_events(), 'events_swarm.pf')
    io.save(response.pyrocko_traces(), 'swarm.mseed')

    convolved_traces = stf.post_process(response)

    # Save traces:
    io.save(convolved_traces.traces_list(), 'swarm_stf.mseed')
Exemplo n.º 5
0
    def plot_tuning_vs_conn_cg(self, conn_type, show=False):
        """
        For each source cell, loop through all target connections and compute the
        scalar (dot) product between the preferred direction of the source cell and the center of gravity of the connection vector
        (both in the spatial domain and the direction domain)
        c_x_i = sum_j w_ij * (x_i - x_j) # x_ are position vectors of the cell
        c_v_i = sum_j w_ij * (v_i - v_j) # v_ are preferred directions
        """
        (n_src, n_tgt, tp_src, tp_tgt) = utils.resolve_src_tgt_with_tp(conn_type, self.params)
        fn = self.params['merged_conn_list_%s' % conn_type]
        print 'Loading:', fn
        if not os.path.exists(fn):
            print 'Merging connlists ...'
            cmd = 'python merge_connlists.py %s' % self.params['params_fn']
            os.system(cmd)

        conn_list = np.loadtxt(fn)

#        conn_mat_fn = self.params['conn_mat_fn_base'] + '%s.dat' % (conn_type)
#        if os.path.exists(conn_mat_fn):
#            print 'Loading', conn_mat_fn
#            w = np.loadtxt(conn_mat_fn)
#        else:
#            w, delays = utils.convert_connlist_to_matrix(params['merged_conn_list_%s' % conn_type], n_src, n_tgt)
#            print 'Saving:', conn_mat_fn
#            np.savetxt(conn_mat_fn, w)

        # for all source cells store the length of the vector:
        # (connection centroid - preferred direction)
        diff_conn_centroid_x_vsrc = np.zeros(n_src)
        diff_conn_centroid_v_vsrc = np.zeros(n_src)
        angles_x = np.zeros(n_src)
        angles_v = np.zeros(n_src)
        # deprecated
#        cx_ = np.zeros(n_src) # stores the scalar products
#        cv_ = np.zeros(n_src) # stores the scalar products
        for i in xrange(n_src):
            src_gid = i
            targets = utils.get_targets(conn_list, src_gid)
            weights = targets[:, 2]
            targets = np.array(targets[:, 1], dtype=np.int)
#            weights = w[src_gid, targets]
            if weights.size > 0:
                c_x, c_v = self.get_cg_vec(tp_src[src_gid, :], tp_tgt[targets, :], weights)
            else:
                c_x, c_v = [(0, 0), (0, 0)]

            (x_src, y_src, vx_src, vy_src) = tp_src[src_gid, :]
#            cx_[i] = np.abs(np.dot(c_x, (vx_src, vy_src)))
#            cv_[i] = np.abs(np.dot(c_v, (vx_src, vy_src)))

            vector_conn_centroid_x_minus_vsrc = (c_x[0] - vx_src, c_x[1] - vy_src)
            vector_conn_centroid_v_minus_vsrc = (c_v[0] - vx_src, c_v[1] - vy_src)
#            angles_x[i] = np.arc((c_x[
            diff_conn_centroid_x_vsrc[i] = np.sqrt(np.dot(vector_conn_centroid_x_minus_vsrc, vector_conn_centroid_x_minus_vsrc))
            diff_conn_centroid_v_vsrc[i] = np.sqrt(np.dot(vector_conn_centroid_v_minus_vsrc, vector_conn_centroid_v_minus_vsrc))


        print 'diff_conn_centroid_x_vsrc mean %.2e +- %.2e' % (diff_conn_centroid_x_vsrc.mean(), diff_conn_centroid_x_vsrc.std())
        print 'diff_conn_centroid_v_vsrc mean %.2e +- %.2e' % (diff_conn_centroid_v_vsrc.mean(), diff_conn_centroid_v_vsrc.std())
#        cx_mean = cx_.mean()
#        cx_sem = cx_.std() / np.sqrt(cx_.size)
#        cv_mean = cv_.mean()
#        cv_sem = cv_.std() / np.sqrt(cv_.size)
        cx_mean = diff_conn_centroid_x_vsrc.mean()
        cx_sem = diff_conn_centroid_x_vsrc.std() / np.sqrt(n_src)
        cv_mean = diff_conn_centroid_v_vsrc.mean()
        cv_sem = diff_conn_centroid_v_vsrc.std() / np.sqrt(n_src)

        output_fn = self.params['data_folder'] + 'mean_length_of_vector_diff_tuning_prop_minus_cgxv.dat'
        output_data = np.array((diff_conn_centroid_x_vsrc, diff_conn_centroid_v_vsrc)).transpose()
#        output_data = np.array((diff_conn_centroid_x_vsrc, diff_conn_centroid_v_vsrc, cx_, cv_)).transpose()
        print 'Saving to:', output_fn
        np.savetxt(output_fn, output_data)

        fig = pylab.figure(figsize=(12, 10))
        pylab.subplots_adjust(hspace=0.35)
        ax1 = fig.add_subplot(211)
        ax2 = fig.add_subplot(212)
        x = range(n_src)
        ax1.set_xlabel('source cell')
        ax1.set_ylabel('$|\\vec{v}_i - \\vec{c}_i^X|$')
        title = '$\langle|\\vec{v}_i - \\vec{c}_i^X| \\rangle = %.2e \pm %.1e$' % (cx_mean, cx_sem)
        ax1.bar(x, diff_conn_centroid_x_vsrc)
        ax1.set_title('Length of difference vector: preferred direction $\\vec{v}_i$ and connection centroid $\\vec{c}_i^x$\n%s' % title)
        ax1.set_xlim((0, n_src))
#        ax1.legend()

        ax2.bar(x, diff_conn_centroid_v_vsrc)
        ax2.set_xlabel('source cell')
        ax1.set_ylabel('$|\\vec{v}_i - \\vec{c}_i^V|$')
        title = '$\langle|\\vec{v}_i - \\vec{c}_i^V| \\rangle = %.2e \pm %.1e$' % (cv_mean, cv_sem)
        ax2.set_title(title)
        ax2.set_xlim((0, n_src))
#        ax2.legend()
        output_fig = self.params['figures_folder'] + 'mean_length_of_vector_diff_tuning_prop_minus_cgxv.png'
        print 'Saving to:', output_fig
        pylab.savefig(output_fig)
        if show:
            pylab.show()
Exemplo n.º 6
0
    def plot_tgt_connections(self, conn_type, gids_to_plot=None, fig_cnt=1):
        """
        For all gids_to_plot all outgoing connections and the centroid / center of gravitiy is plotted.
        conn_type = ['ee', 'ei', 'ie', 'ii']
        """

        tp_src, tp_tgt = self.get_tp(conn_type)
        if gids_to_plot == None:
            if conn_type[0] == 'e':
                gids_to_plot = np.loadtxt(self.params['gids_to_record_fn'], dtype=np.int)
                gids_to_plot = [gids_to_plot[0]]
            else:
                gids_to_plot = np.random.randint(0, tp_src[:, 0].size, 1)

        fn = self.params['merged_conn_list_%s' % conn_type]
        print 'Loading:', fn
        if not os.path.exists(fn):
            print 'Merging connlists ...'
            cmd = 'python merge_connlists.py %s' % self.params['params_fn']
            os.system(cmd)

        if not self.conn_lists.has_key(conn_type):
            self.load_connlist(conn_type)
        conn_list = self.conn_lists[conn_type]

        ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
        for i_, src_gid in enumerate(gids_to_plot):
            (x, y, u, v) = tp_src[src_gid, :]

            tgts = utils.get_targets(conn_list, src_gid)
            tgt_ids = np.array(tgts[:, 1], dtype=np.int)
            weights = tgts[:, 2]
            delays = tgts[:, 3]
            print 'weights size', weights.size
            if weights.size > 0:
                c_x, c_v = self.get_cg_vec(tp_src[src_gid, :], tp_tgt[tgt_ids, :], weights)
                markersizes = utils.linear_transformation(weights, self.markersize_min, self.markersize_max)
            else:
                print '\n WARNING: Cell %d has no outgoing connections!\n' % src_gid
                c_x, c_v = [(0, 0), (0, 0)]
                markersizes = []

            vector_conn_centroid_x_minus_vsrc = (c_x[0] - u, c_x[1] - v)
#            c_x *= 100.
            for j_, tgt_gid in enumerate(tgts[:, 1]):
                (x_tgt, y_tgt, u_tgt, v_tgt) = tp_tgt[tgt_gid, :]
                xdiff = (x_tgt - x)
                ydiff = (y_tgt - y)
                ax.plot(xdiff, ydiff, 'o', markersize=markersizes[j_], color='r')
#                ax.quiver(
            preferred_direction = ax.quiver(0, 0, u, v, angles='xy', scale_units='xy', scale=1, color='r', headwidth=3, width=self.shaft_width * 2, linewidths=(1,), edgecolors=('k'), zorder=100000)
            connection_centroid = ax.quiver(0, 0, c_x[0], c_x[1], angles='xy', scale_units='xy', scale=1, color='k', headwidth=3, width=self.shaft_width * 2, linewidths=(1,), edgecolors=('k'), zorder=100000)
            diff_v = ax.quiver(0, 0, vector_conn_centroid_x_minus_vsrc[0], vector_conn_centroid_x_minus_vsrc[1], angles='xy', scale_units='xy', scale=1, color='y', headwidth=3, width=self.shaft_width * 2, linewidths=(1,), edgecolors=('k'), zorder=100000)
            xlim = ax.get_xlim()
            ylim = ax.get_ylim()
            ax.plot((0, 0), (ylim[0], ylim[1]), 'k--')
            ax.plot((xlim[0], xlim[1]), (0, 0), 'k--')
            quiverkey_length = .05 * (xlim[1] - xlim[0] + ylim[1] - ylim[0])
            ax.quiverkey(preferred_direction, .1, .85, quiverkey_length, 'Preferred direction')
            ax.quiverkey(connection_centroid, .1, .75, quiverkey_length, 'Connection centroid')
            ax.quiverkey(diff_v, .8, .95, quiverkey_length, 'Difference vector')
Exemplo n.º 7
0
        draw_it = False


    try:
        draw_rank = int(sys.argv[4])
    except IndexError:
        draw_rank = size/2



    epsilon = 0.001

    g, n, m, s = get_graph(name, size=size)


    ts = get_targets(g, s, [ 2**i for i in xrange(4, int(math.log(n, 2))) if 2**i < n ])
    if draw_it:
        ts = get_targets(g, s, [draw_rank])
    algorithms = [ a_star_bidirectional ]      
#    algorithms = [ dijkstra_cancel, dijkstra_bidirectional, a_star, a_star_bidirectional  ]
#    algorithms = [ dijkstra_cancel, dijkstra_bidirectional, dijkstra_bidirectional_mue, a_star,
#                   a_star_bidirectional, a_star_bidirectional_onesided, a_star_bidirectional_betterpi, cheater ]
    results = defaultdict(list)
    base_results = []
    sys.stdout.write('running ')
    count = 0
    for t, rank in ts:
        base_results.append(dijkstra_cancel(g, s, t))
        for algorithm in algorithms:
            result = algorithm(g, s, t)
            results[algorithm].append(result)
Exemplo n.º 8
0
                        batch_size=128,
                        shuffle=True,
                        num_workers=0)

start_time = time.time()
cost_fn = nn.BCELoss()
sig = nn.Sigmoid()
for epoch in range(EPOCHS):
    model.train()

    for batch_idx, (img, gt) in enumerate(train_loader):

        optimizer.zero_grad()
        # FORWARD AND BACK PROP
        x = model(img)
        target = utils.get_targets(gt).to(model.device)
        cost = cost_fn(sig(x), target)
        cost.backward()

        # UPDATE MODEL PARAMETERS
        optimizer.step()
        if not batch_idx % 10:
            s = (
                'Batch:{}/{} | Epoch: {}/{} | Cost: {:.4f} | Time elapsed: {:.2f} min'
                .format(batch_idx, len(train_loader), epoch, args.epochs, cost,
                        (time.time() - start_time) / 60))
            print(s)

            with open(LOGFILE, 'a') as f:
                f.write('{}\n'.format(s))
Exemplo n.º 9
0
        X['corr'][b] = utils.get_correlation_image(img_stack)
        X['mean'][b] = np.mean(img_stack, 0)
        X['median'][b] = np.median(img_stack, 0)
        X['max'][b] = img_stack.max(0)
        X['std'][b] = img_stack.std(0)

    # collapse across summary images and scale from 0-1
    X['corr'] = utils.scale_img(X['corr'].max(0))
    X['mean'] = utils.scale_img(X['max'].max(0))
    X['median'] = utils.scale_img(X['median'].max(0))
    X['max'] = utils.scale_img(X['max'].max(0))
    X['std'] = utils.scale_img(X['std'].mean(0))

    # get targets
    y = utils.get_targets(
        os.path.join(cfg.data_dir, 'labels', d), collapse_masks=True,
        centroid_radius=3, border_thickness=cfg.border_thickness)

    # get tensor of masks for each individual neuron (used by segmentation network only)
    neuron_masks = utils.get_targets(
        os.path.join(cfg.data_dir, 'labels', d), collapse_masks=False)
    neuron_masks = neuron_masks['somas']  # keep only the soma masks

    # store data for model training
    training_data_folder = os.path.join(cfg.data_dir, 'training_data')
    if not os.path.exists(training_data_folder):
        os.makedirs(training_data_folder)
    np.savez(os.path.join(training_data_folder, d), X=X, y=y, neuron_masks=neuron_masks)

# write sample images to disk
utils.write_sample_imgs(X_contrast=(5, 99))
Exemplo n.º 10
0
    def plot_connection_type(self, src_gid, conn_type, marker, color, outgoing_conns=True, with_directions=False, plot_delays=False, annotate=False, with_histogram=False):
        self.load_connection_list(conn_type)
        if outgoing_conns:
            src_tgts = utils.get_targets(self.connection_lists[conn_type], src_gid)
            tgt_ids, weights, delays = src_tgts[:, 1], src_tgts[:, 2], src_tgts[:, 3]
            print 'Cell %d connects to:' % src_gid, tgt_ids
        else:
            src_tgts = utils.get_sources(self.connection_lists[conn_type], src_gid)
            tgt_ids, weights, delays = src_tgts[:, 0], src_tgts[:, 2], src_tgts[:, 3]
            print 'Cell %d receives input from:' % src_gid, tgt_ids
        if conn_type == 'ee':
            src_tp = self.tp_exc
            tgt_tp = self.tp_exc
            legend_txt = 'exc src gid: %d --> exc tgts, n=%d' % (src_gid, len(tgt_ids))
        elif conn_type == 'ei':
            src_tp = self.tp_exc
            tgt_tp = self.tp_inh
            legend_txt = 'exc src gid: %d --> inh tgts, n=%d' % (src_gid, len(tgt_ids))
        elif conn_type == 'ie':
            src_tp = self.tp_inh
            tgt_tp = self.tp_exc
            legend_txt = 'inh src gid: %d --> exc tgts, n=%d' % (src_gid, len(tgt_ids))
        elif conn_type == 'ii':
            src_tp = self.tp_inh
            tgt_tp = self.tp_inh
            legend_txt = 'inh src gid: %d --> inh tgts, n=%d' % (src_gid, len(tgt_ids))

        if len(tgt_ids) > 0:
            plot = self.plot_connections(tgt_ids, tgt_tp, weights, marker, color, with_directions, annotate, outgoing_conns)
            if outgoing_conns:
                print 'Average weight for outgoing connections: %.2e +- %.2e ' % (weights.mean(), weights.std())
            if outgoing_conns:
                print 'Average weight for outgoing connections: %.2e +- %.2e ' % (weights.mean(), weights.std())
        else:
            return []

        if plot_delays:
            delay_min, delay_max = delays.min(), delays.max()
#            delay_min, delay_max = self.params['delay_range'][0], self.params['delay_range'][1]
            norm = matplotlib.mpl.colors.Normalize(vmin=delay_min, vmax=delay_max)
            m = matplotlib.cm.ScalarMappable(norm=norm, cmap=cm.jet)#spring)
            m.set_array(np.arange(delay_min, delay_max, 0.01))
            if not self.delay_colorbar_set:
                cb = self.fig.colorbar(m)
                cb.set_label('Connection delays [ms]', fontsize=28)
                self.delay_colorbar_set = True

            x_src, y_src = src_tp[src_gid, 0], src_tp[src_gid, 1]
            for i_, tgt_gid in enumerate(tgt_ids):
                x_tgt, y_tgt = tgt_tp[tgt_gid, 0] % self.params['torus_width'], tgt_tp[tgt_gid, 1] % self.params['torus_height']
                c = m.to_rgba(delays[i_])
                self.ax.plot((x_src, x_tgt), (y_src, y_tgt), c=c, lw=2, alpha=.5)

#            s = 1. # saturation
#            for 
#                if activity[frame, tgt_gid] < 0:
#                    l = 1. - 0.5 * activity[frame, tgt_gid] / activity_min
#                    h = 0.
#                else:
#                    l = 1. - 0.5 * activity[frame, tgt_gid] / activity_max
#                    h = 240.
#                assert (0 <= h and h < 360)
#                assert (0 <= l and l <= 1)
#                assert (0 <= s and s <= 1)
#                (r, g, b) = utils.convert_hsl_to_rgb(h, s, l)
#                colors[frame, tgt_gid, :] = [r, g, b]
#                

        if with_histogram:
            print '\nPlotting weight and delay histogram'
            self.plot_weight_and_delay_histogram(weights, delays)

        self.legends.append((plot[0], legend_txt))
        print 'src_gid %d has %d outgoing %s->%s connection' % (src_gid, len(weights), conn_type[0].capitalize(), conn_type[1].capitalize())
        return tgt_ids
Exemplo n.º 11
0
def main():
    """Run the project"""
    start_time = time.time()

    try:
        nltk.data.find('corpora/stopwords')
    except LookupError:
        nltk.download('stopwords')
    try:
        nltk.data.find('tokenizers/punkt')
    except LookupError:
        nltk.download('punkt')

    if not os.path.exists(args.output):
        os.makedirs(args.output)

    save_path = os.path.join(args.output, 'saves')
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    if not os.path.exists(os.path.join(args.output, 'task1')):
        os.makedirs(os.path.join(args.output, 'task1'))
    if not os.path.exists(os.path.join(args.output, 'task2')):
        os.makedirs(os.path.join(args.output, 'task2'))

    print('Loading words...')
    corpus = Corpus(args.start_corpus, save_path,
                    args.end_corpus, args.floor, args.window_size)
    print('Setting up initial partition...')
    for i in corpus.docs:
        i.init_partition(args.alpha)

    hdp = HDP(corpus.vocab_size, save_path,
              alpha=args.alpha, gamma=args.gamma)
    hdp.init_partition(corpus.docs)
    print('Done')
    it = 0
    print(f'Running Gibbs sampling for {args.max_iters} iterations...')
    while it < args.max_iters:
        for j in corpus.docs:
            for i in range(len(j.words)):
                hdp.sample_table(j, i, corpus.collocations[j.words[i]])
        it += 1
        corpus.save()
        print(f'Iteration {it}/{args.max_iters}')
    for i in hdp.senses:
        i /= i.sum()
    print('Done')
    print('Generating scores for word senses...')
    words = dict()
    for j in corpus.docs:
        for i, p in enumerate(j.partition):
            origin = j.category
            sense = j.topic_to_global_idx[i]
            for w in p:
                if corpus.idx_to_word[w] in words:
                    if origin == 'reference':
                        words[corpus.idx_to_word[w]].senses[sense][0] += 1
                    else:
                        words[corpus.idx_to_word[w]].senses[sense][1] += 1
                else:
                    word = Word(corpus.idx_to_word[w], w, hdp.senses.shape[0])
                    if origin == 'reference':
                        word.senses[sense][0] += 1
                    else:
                        word.senses[sense][1] += 1
                    words[word.word] = word
    print('Done.')
    if args.semeval_mode:
        targets = utils.get_targets(args.targets)
        results = []
        for i in range(len(targets)):
            t = targets[i][0]
            pos = targets[i][1]
            recombine = t+'_'+pos
            word = words[recombine]
            scores = word.senses[~np.all(word.senses == 0, axis=1)]

            dist_1 = scores[:, 0]
            dist_2 = scores[:, 1]
            jensenshannon = dist.jensenshannon(
                dist_1, dist_2)
            results.append((recombine, jensenshannon))

        with open(os.path.join(os.path.join(args.output, 'task1'),
                               'english.txt'), 'w') as f:
            for i in results:
                recombine = i[0]
                score = i[1]
                different = 1 if score > args.threshold else 0
                f.write(f'{recombine} {different}\n')

        with open(os.path.join(os.path.join(args.output, 'task2'), 'english.txt'), 'w') as f:
            for i in results:
                recombine = i[0]
                jensenshannon = i[1]
                f.write(f'{recombine} {jensenshannon:.4f}\n')

    else:
        for k, v in words.items():
            words[k] = v.calculate()
        top = sorted(words, key=words.get, reverse=True)[:args.top_k]
        with open(os.path.join(args.output, 'out.txt'), 'w') as f:
            f.write(f'Top {args.top_k} most differing words:')
            f.write('\n'.join(top))
    end_time = time.time()
    print(f'Ran project in {end_time - start_time} seconds')
Exemplo n.º 12
0
if __name__ == '__main__':

    if len(sys.argv) != 3:
        sys.exit("Improper number of args")

    train_path = sys.argv[1]
    #test_path = '/Users/pallekc/Jobs/comma/speed_challenge_2017/data/test.mp4'
    train_targets = sys.argv[2]

    model = get_nvidia_model()
    if t.cuda.is_available():
        model.cuda()
    device = t.device("cuda:0" if t.cuda.is_available() else "cpu")
    print('device ', device)
    train_frames, train_frames_count = get_frames(train_path)
    train_targets, train_targets_count = get_targets(train_targets)
    assert train_frames_count == train_targets_count, 'Number of train frames != targets'

    train_processed = process_frames(
        train_frames, train_targets)  # remember the first one is missing
    train_x, train_y, test_x, test_y = split_data(train_processed)

    train_dataset = SpeedDataset(train_x, train_y)
    training_generator = t.utils.data.DataLoader(train_dataset,
                                                 batch_size=8,
                                                 shuffle=True)

    # train_x, train_y, val_x, val_y = split_data(train_x, train_y)
    # val_dataset = SpeedDataset(val_x, val_y)
    # val_generator = t.utils.data.DataLoader(val_dataset, batch_size=8, shuffle=True)