コード例 #1
0
def add_extra_dims(linear_model):
    color_map = cm.ScalarMappable(cmap="hsv")
    color_array = color_map.to_rgba(linear_model)[:, :-1]
    new_model = np.zeros((color_array.shape[0], 4))
    for x in range(color_array.shape[0]):
        new_model[x] = np.append(color_array[x], linear_model[x])
    return new_model
コード例 #2
0
ファイル: alerttimemap.py プロジェクト: yannikbehr/SRL_2016
    def plot1D(self, scale=True, meridians=np.arange(5, 12, 2),
               parallels=np.arange(44, 49, 2), vmin=0., vmax=15,
               blindzone=False):
        """
        Show alert times for selected events.
        """
        ttPmed = np.median(self.ttP, axis=0)
        if blindzone:
            bz2 = ttPmed * ttPmed * self.vs * self.vs - self.dep * self.dep
            bz2 = np.where(bz2 > 0.0, bz2, 0.0)
            bz = np.sqrt(bz2)
            ttPmed = bz

        cmap = cm.ScalarMappable(norm=Normalize(vmin=vmin, vmax=vmax),
                                 cmap=self.cmapname)
        for lt, ln, delay in zip(self.lat, self.lon, ttPmed):
            cl = cmap.to_rgba(delay)
            x, y = self.m(ln, lt)
            self.m.plot(x, y, ms=8, c=cl, marker='o', picker=5.)
        self.m.drawmeridians(meridians, labels=[0, 0, 0, 1], color='lightgray',
                             linewidth=0.5, zorder=0)
        self.m.drawparallels(parallels, labels=[1, 0, 0, 0], color='lightgray',
                             linewidth=0.5, zorder=0)
        self.m.drawcoastlines(zorder=2)
        self.m.drawcountries(linewidth=1.0, zorder=2)
        self.m.drawstates(zorder=2)
コード例 #3
0
def plotHist(data,
             title,
             countTools,
             xlabel="hour",
             ylabel="count of queries",
             log=False):
    if not os.path.exists(title[:title.rfind("/")]):
        os.makedirs(title[:title.rfind("/")])
    fig = plt.figure(1)
    ax = fig.add_subplot(111)
    plt.grid(True)

    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)

    axes = plt.axes()
    axes.xaxis.set_major_locator(ticker.MultipleLocator(1))

    if log:
        axes.set_yscale('log')

    colormap = cm.nipy_spectral(np.linspace(0, 1, countTools))
    color = iter(colormap)

    minMetricValue = data[0][1]["Y"][0]
    maxMetricValue = 0

    for dataPoint in data:
        c = next((color))
        XY = dataPoint[1]

        if (XY["Y"][0] < minMetricValue):
            minMetricValue = XY["Y"][0]

        if (XY["X"][0] > maxMetricValue):
            maxMetricValue = XY["Y"][0]

        try:
            ax.bar(XY["X"], XY["Y"], align='center', color=c, edgecolor=c)
        except ValueError:
            pass

    scalarMappaple = cm.ScalarMappable(cmap=cm.nipy_spectral,
                                       norm=plt.Normalize(vmin=minMetricValue,
                                                          vmax=maxMetricValue))
    scalarMappaple._A = []
    plt.colorbar(scalarMappaple)

    if xlabel is 'hour':
        plt.xlim(-1, 24)

    if xlabel is 'day':
        plt.xlim(0, 32)

    plt.xticks(fontsize=9)

    plt.savefig(title + ".png", bbox_inches='tight')

    plt.close()
コード例 #4
0
def plot_densities_on_map_by_time_point(population_data, time):
    plt.figure(figsize=(14, 8))
    my_map = Basemap(projection='robin',
                     lat_0=57,
                     lon_0=-135,
                     resolution='l',
                     area_thresh=1000.0,
                     llcrnrlon=-136.25,
                     llcrnrlat=56,
                     urcrnrlon=-134.25,
                     urcrnrlat=57.75)

    my_map.drawcoastlines()
    my_map.drawcountries()
    my_map.fillcontinents(color='lightgray', zorder=0)
    my_map.drawmapboundary()

    my_map.drawmeridians(np.arange(0, 360, 30))
    my_map.drawparallels(np.arange(-90, 90, 30))

    time_index = -1
    for i in range(0, len(population_data.time_array)):
        if time == population_data.time_array[
                i] * population_data.time_multiplier:
            time_index = i
            break

    lats = []
    lons = []
    dens = []
    for i in range(0, len(population_data.density_array[time_index])):
        density = population_data.density_array[time_index][i]
        if density != 0:
            lats.append(population_data.lat_array[i])
            lons.append(population_data.lon_array[i])
            dens.append(density)

    cmap = cm.get_cmap('jet')

    dens = np.array(dens).astype(float) / 3000
    x, y = my_map(lons, lats)
    rgba = cmap(dens)

    ax = my_map.scatter(x, y, marker='o', c=rgba, s=3, zorder=1)

    sm = cm.ScalarMappable(cmap=cmap)
    sm.set_array([])

    sm.set_clim([0, 3000])
    plt.colorbar(sm, orientation='horizontal', pad=0.03, aspect=50)

    plt.savefig("densitites_on_map_" + population_data.name + "_" + str(time) +
                "Kya.png")

    map_path = get_map_file_path(population_data.name.lower() +
                                 "_densitites_on_map_" + str(time) + ".png")
    print("Map filepath: " + map_path)
    plt.savefig(map_path, dpi=500)
    plt.close()
コード例 #5
0
def make_brain_image(views, brain, orient="horizontal", text="",
                     text_loc=None, text_pan=None, fontsize=160,
                     legend=None, legend_pan=None, cbar=None,
                     vmin=None, vmax=None, cbar_label=""):
    img_list = []
    axis = 1 if orient=="horizontal" else 0
    for k,v in views.items():
        brain.show_view(**v)
        scr = brain.screenshot()
        img_list.append(scr)
    if text != "":
        img_txt_list = []
        brain.add_text(0, 0.8, text, text_loc, font_size=fontsize, color=(0,0,0))
        for k,v in views.items():
            brain.show_view(**v)
            scr = brain.screenshot()
            img_txt_list.append(scr)
        img_list[text_pan] = img_txt_list[text_pan]
    if legend:
        legend_list = []
        brain._renderer.plotter.add_legend(legend, bcolor=(1,1,1))
        for k,v in views.items():
            brain.show_view(**v)
            scr = brain.screenshot()
            legend_list.append(scr)
        img_list[legend_pan] = legend_list[legend_pan]
    if orient == "square": # only works for 2x2
        h, w, _ = img_list[0].shape
        img = np.zeros((h*2, w*2, 3), dtype=np.uint8)
        img[:h, :w, ] = img_list[0]
        img[:h, w:, ] = img_list[1]
        img[h:, :w, ] = img_list[2]
        img[h:, w:, ] = img_list[3]
    else:
        img = np.concatenate(img_list, axis=axis)

    if cbar:
        norm = Normalize(vmin, vmax)
        scalmap = cm.ScalarMappable(norm, cbar)
        if orient == "horizontal":
            colbar_size = (img_list[-1].shape[0]*4, img_list[-1].shape[1]/6)
        else:
            colbar_size = (img_list[-1].shape[0]/6, img_list[-1].shape[1]*4)
        colbar_size = np.array(colbar_size) / 100
        fig, ax = plt.subplots(1,1, figsize=colbar_size)
        colbar = plt.colorbar(scalmap, cax=ax, orientation=orient)
        ax.tick_params(labelsize=48)
        if orient == "horizontal":
            ax.set_xlabel(cbar_label, fontsize=48)
        else:
            ax.set_ylabel(cbar_label, fontsize=48)
        fig.tight_layout()
        fig.canvas.draw()
        mat = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
        mat = mat.reshape(fig.canvas.get_width_height()[::-1] + (3,))
        img = np.concatenate((img, mat), axis=abs(axis-1))

    return img
コード例 #6
0
ファイル: visualizer.py プロジェクト: Na-Z/3DMVGOD
def visualize_frustum_ptcloud_with_cam(frustum_ptcloud):
    '''
    :param frustum_ptcloud: np.ndarray, shape (n,4) [x,y,z,s], s is the class activation score \in (0,1)
    '''
    cm = colormap.get_cmap('RdBu')  #inferno
    color_mapper = colormap.ScalarMappable(norm=matplotlib.colors.Normalize(
        vmin=0, vmax=1, clip=True),
                                           cmap=cm)
    color = color_mapper.to_rgba(frustum_ptcloud[:, 3])[:, :3] * 255
    visualize_ptcloud_with_color(frustum_ptcloud[:, 0:3], color)
コード例 #7
0
def plot_optimized_network(network, blocking=True, save_plot=True):
    _title = 'Optimized network vs. non-optimized'
    figure(_title)

    graph = network.network_plot
    plot_layout = spring_layout(graph)

    balls = network.get_ball_distribution()

    print(network.ref_distribution)
    print(balls)

    min_val = 0
    max_val = max([np_max(network.ref_distribution), np_max(balls)])

    cmap = cm.Greys
    color_vals = cm.ScalarMappable(cmap=cmap,
                                   norm=Normalize(vmin=min_val, vmax=max_val))
    color_vals._A = []

    subplot(2, 1, 1)
    title('Initial network')
    colorbar(color_vals)

    draw_networkx_edges(graph, plot_layout, alpha=.3)
    draw_networkx_nodes(graph,
                        plot_layout,
                        node_size=100,
                        edgecolors='k',
                        node_color=network.ref_distribution,
                        cmap=cmap,
                        vmin=min_val,
                        vmax=max_val)
    axis('off')  # Disable axis

    subplot(2, 1, 2)
    title('Optimized network')
    colorbar(color_vals)

    draw_networkx_edges(graph, plot_layout, alpha=.3)
    draw_networkx_nodes(graph,
                        plot_layout,
                        node_size=100,
                        edgecolors='k',
                        node_color=balls,
                        cmap=cmap,
                        vmin=min_val,
                        vmax=max_val)
    axis('off')
    draw()

    if save_plot:
        savefig('../results/optimized_network.png')
    show(block=blocking)  # Open matplotlib window
コード例 #8
0
ファイル: train.py プロジェクト: zhouyao4321/RPMNet
def save_summaries(writer: SummaryWriter, data: Dict, predicted: List, endpoints: Dict = None,
                   losses: Dict = None, metrics: Dict = None, step: int = 0):
    """Save tensorboard summaries"""

    subset = [0, 1]

    with torch.no_grad():
        # Save clouds
        if 'points_src' in data:

            points_src = data['points_src'][subset, ..., :3]
            points_ref = data['points_ref'][subset, ..., :3]

            colors = torch.from_numpy(
                np.concatenate([np.tile(ORANGE, (*points_src.shape[0:2], 1)),
                                np.tile(BLUE, (*points_ref.shape[0:2], 1))], axis=1))

            iters_to_save = [0, len(predicted)-1] if len(predicted) > 1 else [0]

            # Save point cloud at iter0, iter1 and after last iter
            concat_cloud_input = torch.cat((points_src, points_ref), dim=1)
            writer.add_mesh('iter_0', vertices=concat_cloud_input, colors=colors, global_step=step)
            for i_iter in iters_to_save:
                src_transformed_first = se3.transform(predicted[i_iter][subset, ...], points_src)
                concat_cloud_first = torch.cat((src_transformed_first, points_ref), dim=1)
                writer.add_mesh('iter_{}'.format(i_iter+1), vertices=concat_cloud_first, colors=colors, global_step=step)

            if endpoints is not None and 'perm_matrices' in endpoints:
                color_mapper = colormap.ScalarMappable(norm=None, cmap=colormap.get_cmap('coolwarm'))
                for i_iter in iters_to_save:
                    ref_weights = torch.sum(endpoints['perm_matrices'][i_iter][subset, ...], dim=1)
                    ref_colors = color_mapper.to_rgba(ref_weights.detach().cpu().numpy())[..., :3]
                    writer.add_mesh('ref_weights_{}'.format(i_iter), vertices=points_ref,
                                    colors=torch.from_numpy(ref_colors) * 255, global_step=step)

        if endpoints is not None:
            if 'perm_matrices' in endpoints:
                for i_iter in range(len(endpoints['perm_matrices'])):
                    src_weights = torch.sum(endpoints['perm_matrices'][i_iter], dim=2)
                    ref_weights = torch.sum(endpoints['perm_matrices'][i_iter], dim=1)
                    writer.add_histogram('src_weights_{}'.format(i_iter), src_weights, global_step=step)
                    writer.add_histogram('ref_weights_{}'.format(i_iter), ref_weights, global_step=step)

        # Write losses and metrics
        if losses is not None:
            for l in losses:
                writer.add_scalar('losses/{}'.format(l), losses[l], step)
        if metrics is not None:
            for m in metrics:
                writer.add_scalar('metrics/{}'.format(m), metrics[m], step)

        writer.flush()
コード例 #9
0
def make_brain_figure(views, brain, cbar=None, vmin=None, vmax=None,
                      cbar_label="", cbar_orient="vertical",
                      hide_cbar=False):
    img_list = []
    for k,v in views.items():
        brain.show_view(**v)
        scr = brain.screenshot()
        img_list.append(scr)

    width = 3*scr.shape[0]/100
    height = scr.shape[1]/100

    cb_ratio = 6
    pans = ["A", "B", "C"]
    if cbar:
        width += width * 1/cb_ratio
        mos_str = ""
        if cbar_orient == "vertical":
            for pan in pans:
                for x in range(cb_ratio):
                    mos_str += pan
            mos_str += "D"
        elif cbar_orient == "horizontal":
            for x in range(cb_ratio):
                for pan in pans:
                    mos_str += pan
                mos_str += "\n"
            mos_str += "D"*len(pans)
        fig, axes = plt.subplot_mosaic(mos_str, figsize=(width, height))
        for img_mos, img in zip(pans, img_list):
            axes[img_mos].imshow(img)
            axes[img_mos].axis("off")
        if not hide_cbar:
            norm = Normalize(vmin, vmax)
            scalmap = cm.ScalarMappable(norm, cbar)
            plt.colorbar(scalmap, cax=axes["D"], orientation=cbar_orient)
            axes["D"].tick_params(labelsize=48)
            if cbar_orient == "horizontal":
                axes["D"].set_xlabel(cbar_label, fontsize=48)
            else:
                axes["D"].set_ylabel(cbar_label, fontsize=48)
        else:
            axes["D"].axis("off")
    else:
        fig, axes = plt.subplots(1, 3, figsize=(width, height))
        for ax, img in zip(axes, img_list):
            ax.imshow(img)
            ax.axis("off")

    return fig
コード例 #10
0
ファイル: network.py プロジェクト: simonsben/undergrad_thesis
def plot_network(network,
                 blocking=True,
                 netx_plot=False,
                 size=fig_size,
                 weights=None,
                 file_name=None,
                 plot_edges=False,
                 alph=.05):
    fig = figure(figsize=size)
    rcParams.update({
        'font.size': plot_font_size,
        'mathtext.default': 'regular'
    })
    ax = fig.gca()
    ax.axis('off')  # Disable axis

    graph = network if netx_plot else network.network_plot
    plot_layout = kamada_kawai_layout(graph)
    cmap = cm.get_cmap('coolwarm')

    sizes, edge_colors, node_colors = 80, 'k', 'w'
    if weights is not None:
        sizes = [50 if weight == 0 else 80 for weight in weights]
        node_colors = weights

        min_val, max_val = min(weights), max(weights)

    if plot_edges: draw_networkx_edges(graph, plot_layout, alpha=alph)
    draw_networkx_nodes(graph,
                        plot_layout,
                        node_size=sizes,
                        linewidths=.5,
                        edgecolors='k',
                        node_color=node_colors,
                        cmap=cmap)
    draw()

    if weights is not None:
        plt = cm.ScalarMappable(cmap=cmap, norm=Normalize(vmin=0, vmax=1))
        plt._A = []
        colorbar(plt)

    if file_name is not None:
        savefig(file_name, bbox_inches='tight', pad_inches=0)
    show(block=blocking)  # Open matplotlib window
コード例 #11
0
ファイル: movie_subs.py プロジェクト: slava-kerner/subsync
 def visualise_signature_distances(self):
     """ plots distances between, colored with green=identical, red=different. """
     dist = self.signature_distances
     ids = sorted(dist.keys())
     xs = []
     ys = []
     colors = []
     colormap = cm.ScalarMappable(cmap='RdYlGn',
                                  norm=plt.Normalize(vmin=0, vmax=1))
     for x, x_id in enumerate(ids):
         for y, y_id in enumerate(ids):
             xs.append(x)
             ys.append(y)
             score = 1 - dist[x_id][y_id]
             colors.append(colormap.to_rgba(score))
     plt.scatter(xs, ys, c=colors)
     plt.title('movie: "%s"' % self.movie.name)
     plt.show()
コード例 #12
0
def plot_layer_2d(mat):

    import matplotlib.pyplot as plt
    from matplotlib.pyplot import cm

    fig = plt.figure()
    ax = fig.add_subplot(111)

    x, y = np.meshgrid(range(mat.shape[1]), range(mat.shape[0]))
    xs = x.reshape(-1)
    ys = y.reshape(-1)
    cols = mat.reshape(-1) - np.min(mat)
    colors = cm.hsv(cols / max(cols))

    colormap = cm.ScalarMappable(cmap=cm.hsv)
    colormap.set_array(cols)
    ax.scatter(xs, ys, c=colors)
    cb = fig.colorbar(colormap)
    plt.title("subtracted:{}".format(np.min(mat)))
    plt.show()
コード例 #13
0
ファイル: visualize.py プロジェクト: yannikbehr/spectroscopy
def plot_rawdata(r, savefig=None, **kargs):
    matplotlib.style.use('ggplot')
    try:
        dmin = kargs['datemin']
        dmax = kargs['datemax']
    except KeyError:
        idx = np.arange(r.d_var.shape[0])
    else:
        try:
            dt = r.datetime[:].astype('datetime64[ms]')
            idx = np.where(((dt > np.datetime64(dmin))
                            & (dt < np.datetime64(dmax))))[0]
        except tables.NoSuchNodeError:
            idx = np.arange(r.d_var.shape[0])
    counts = r.d_var[idx, :]
    w = r.ind_var[:]
    nc = counts.shape[0]
    cmap = cm.ScalarMappable(norm=Normalize(vmin=0, vmax=nc-1), cmap='RdBu')
    fig = plt.figure(figsize=(12, 6))
    for i in range(nc):
        c = cmap.to_rgba(i)
        plt.plot(w, counts[i], color=c, alpha=0.2)

    plt.xlabel('Wavelength [nm]')
    plt.ylabel('Intensity')
    cax, kw = matplotlib.colorbar.make_axes(plt.gca())
    norm = Normalize(vmin=0, vmax=nc, clip=False)
    c = matplotlib.colorbar.ColorbarBase(cax, cmap='RdBu', norm=norm)
    ticks = np.array([0, int(nc/2.), nc-1])
    c.set_ticks(ticks)
    try:
        times = r.datetime[idx]
        labels = np.array([times[0], times[int(nc/2.)], times[nc-1]])
        c.set_ticklabels(labels)
    except tables.NoSuchNodeError:
        pass
    if savefig is not None:
        plt.savefig(
            savefig, bbox_inches='tight', dpi=300, format='png')
    return fig
コード例 #14
0
def plot_layer_3d(mat):

    import matplotlib.pyplot as plt
    from matplotlib.pyplot import cm
    from mpl_toolkits.mplot3d import Axes3D

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    x, y, z = np.meshgrid(range(mat.shape[1]), range(mat.shape[0]),
                          range(mat.shape[2]))
    xs = x.reshape(-1)
    ys = y.reshape(-1)
    zs = z.reshape(-1)
    cols = mat.reshape(-1) - np.min(mat)
    colors = cm.hsv(cols / max(cols))

    colormap = cm.ScalarMappable(cmap=cm.hsv)
    colormap.set_array(cols)
    ax.scatter(xs, ys, zs, c=colors)
    cb = fig.colorbar(colormap)
    plt.title("subtracted:{}".format(np.min(mat)))
    plt.show()
コード例 #15
0
 def __init__(self, cmap, clim):
     self.colormap = cm.ScalarMappable(cmap=cmap, norm=Normalize(*clim))
コード例 #16
0
ファイル: utils.py プロジェクト: canh-ng/dissertation
 def add_rgb_prostate(array):
     scaler_map = cm.ScalarMappable(cmap="Reds")
     array = scaler_map.to_rgba(array)[:, : -1]
     return array
コード例 #17
0
ファイル: utils.py プロジェクト: canh-ng/dissertation
 def add_rgb_organs(array):
     scaler_map = cm.ScalarMappable(cmap="Blues")
     array = scaler_map.to_rgba(array)[:, : -1]
     return array
コード例 #18
0
    def alert_times_map(self,
                        fns,
                        m=None,
                        fig=None,
                        ax=None,
                        scale=10000.,
                        cb=True,
                        disterr=False,
                        interactive=False,
                        eventinfo=None,
                        msscale=1,
                        cmapname='jet'):
        """
        Plot a map of observed alert times.
        """
        cmap = cm.ScalarMappable(norm=Normalize(vmin=6, vmax=25),
                                 cmap=cmapname)
        rp = ReportsParser(dmin=UTCDateTime(2012, 1, 1, 0, 0, 0),
                           dmax=UTCDateTime(2013, 11, 1, 0, 0, 0))
        t = EventCA()
        rp.sfilter = t.point_in_polygon

        for _f in fns:
            rp.read_reports(_f)

        correct = rp.get_correct(mmin=3.5, mmax=10.0)
        pid = correct[:, 0]
        ot = correct[:, 2].astype('float')
        lats = correct[:, 3].astype('float')
        lons = correct[:, 4].astype('float')
        mags = correct[:, 6].astype('float')
        ts1 = correct[:, 7].astype('float')
        lats1 = correct[:, 9].astype('float')
        lons1 = correct[:, 10].astype('float')
        mags1 = correct[:, 12].astype('float')
        rfns = correct[:, 21]
        diff = ts1 - ot
        magdiff = mags - mags1

        if m is None and fig is None and ax is None:
            fig = plt.figure()
            ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
            m = self.background_map(ax)
        dataX = []
        dataY = []
        values = []
        # load event info
        cnt = 0
        allcnt = 0
        for lon, lat, delay, evid, lat1, lon1, dmag, time, mag, rfn in \
            zip(lons, lats, diff, pid, lats1, lons1, magdiff, ot, mags, rfns):
            allcnt += 1
            try:
                if eventinfo is not None and len(eventinfo[evid]) != 4:
                    print "Event %s does not have 4 initial picks." % evid
                    continue
            except KeyError:
                print "No event information available for: %s (%s)" % (
                    evid, UTCDateTime(time))
                continue
            if evid in self.event_excludes:
                print "Event %s was set to be excluded." % evid
                continue
            cnt += 1
            ddist, az, baz = gps2DistAzimuth(lat, lon, lat1, lon1)
            ddist /= 1000.
            x, y = m(lon, lat)
            dataX.append(x)
            dataY.append(y)
            info = '%s: %.2f %.2f %s' % (UTCDateTime(time), delay, mag, evid)
            for _st in eventinfo[evid]:
                info += ' %s' % _st
            values.append(info)
            cl = cmap.to_rgba(delay)
            if disterr:
                factor = math.sqrt(abs(float(ddist)))
                sl2 = scale * factor
                p2 = Wedge((x, y),
                           sl2,
                           0,
                           360,
                           facecolor=cl,
                           edgecolor='black',
                           picker=5,
                           lw=1.0)
                ax.add_patch(p2)
            else:
                m.plot(x, y, ms=8 * msscale, c=cl, marker='o', picker=5.)
        print "Plotted %d out of %d events." % (cnt, allcnt)
        if interactive:
            self.popup(fig, dataX, dataY, values)
        if cb:
            # Colorbar
            cax = fig.add_axes([0.87, 0.1, 0.05, 0.8])
            cb = ColorbarBase(cax,
                              cmap=cmapname,
                              norm=Normalize(vmin=6., vmax=25.))
            cb.set_label('Time since origin time [s]')
コード例 #19
0
                _PATH = 'data/' + i + "_" + j + "_" + o + "_" + l + ".txt"
                #print _PATH
                f = np.loadtxt(_PATH, delimiter='\t', usecols=[0, 1, 2, 3])
                ID = np.loadtxt(_PATH, delimiter='\t', usecols=[7], dtype=str)
                if ID[0] != "":
                    a = f[3:, :]
                else:
                    a = f[:, :]

                fig = plt.figure()
                ax = fig.add_subplot(111, projection='3d')
                #ax.scatter(a[:,0],a[:,1],a[:,2])
                a[:, 3] = 0
                #create color scale
                color_norm = colors.Normalize(vmin=0, vmax=len(a) - 1)
                scalar_map = cm.ScalarMappable(norm=color_norm, cmap='hsv')
                print len(a)
                for t in range(0, len(a)):
                    #calculate the Euclidian distance(ED) between all markers
                    #if distance is under a upper, create an ID
                    for u in range(0, len(a)):
                        ED = np.sqrt((a[t, 0] - a[u, 0])**2 +
                                     (a[t, 1] - a[u, 1])**2 +
                                     (a[t, 2] - a[u, 2])**2)
                        if ED <= _BANDWIDTH and a[u, 3] == 0:
                            a[u, 3] = t
                    #set different colors
                    col = scalar_map.to_rgba(a[t, 3])
                    if (a[t, 3] % 3) != 0 and a[t, 3] != 1:
                        ax.scatter(a[t, 0],
                                   a[t, 1],
コード例 #20
0
    def plot_samples(self,
                     title,
                     inputs,
                     outputs=None,
                     predictions=None,
                     show=True,
                     filename=None,
                     interactive=False,
                     figsize=(10, 6)):
        """Plot samples belonging to this dataset.

        Args:
            (....): See docstring of method
                :meth:`data.dataset.Dataset.plot_samples`.
        """
        if inputs.shape[1] != 2:
            raise ValueError(
                'This method is only applicable to 2D input data!')

        plt.figure(figsize=figsize)
        plt.title(title, size=20)
        if interactive:
            plt.ion()

        if self.classification:
            n = self.num_classes
            colors = np.asarray(misc.get_colorbrewer2_colors(family='Dark2'))
            if n > len(colors):
                #warn('Changing to automatic color scheme as we don\'t have ' +
                #     'as many manual colors as tasks.')
                colors = cm.rainbow(np.linspace(0, 1, n))
        else:
            norm = Normalize(vmin=self._data['out_data'].min(),
                             vmax=self._data['out_data'].max())
            cmap = cm.get_cmap(name='viridis')

            sm = cm.ScalarMappable(norm=norm, cmap=cmap)
            sm.set_array(np.asarray([norm.vmin, norm.vmax]))
            if outputs is not None or predictions is not None:
                plt.colorbar(sm)

        if outputs is not None and predictions is None:
            plt.scatter(inputs[:, 0], inputs[:, 1], #edgecolors='b',
                label='Targets',
                facecolor=colors[outputs.squeeze().astype(int)] \
                    if self.classification else \
                        cmap(norm(outputs.squeeze()))
                )
        elif predictions is not None and outputs is None:
            plt.scatter(inputs[:, 0], inputs[:, 1], #edgecolors='r',
                label='Predictions',
                facecolor=colors[predictions.squeeze().astype(int)] \
                    if self.classification else \
                        cmap(norm(predictions.squeeze()))
                )
        elif predictions is not None and outputs is not None:
            plt.scatter(inputs[:, 0], inputs[:, 1], label='Targets+Predictions',
                edgecolors=colors[outputs.squeeze().astype(int)] \
                    if self.classification else \
                        cmap(norm(outputs.squeeze())),
                facecolor=colors[predictions.squeeze().astype(int)] \
                    if self.classification else \
                        cmap(norm(predictions.squeeze()))
                )
        else:
            assert predictions is None and outputs is None
            plt.scatter(inputs[:, 0], inputs[:, 1], color='k', label='Inputs')

        #plt.legend()
        plt.xlabel('x1')
        plt.ylabel('x2')

        if filename is not None:
            plt.savefig(filename, bbox_inches='tight')

        if show:
            plt.show()
コード例 #21
0
    def statistics(self,
                   fns,
                   fn,
                   stationfn,
                   eventinfo=None,
                   latencies=None,
                   computedelay=False,
                   map=False,
                   interactive=False):
        """
        Compare predicted and observed alert times quantitatively.
        """
        a = np.load(fn)
        lats_tt = a['lat'][:, :, 0]
        lons_tt = a['lon'][:, :, 0]
        times = np.median(a['ttP'], axis=-1)
        tree = spatial.KDTree(zip(lats_tt.ravel(), lons_tt.ravel()))
        vals = []
        perc_max = 84
        perc_min = 16

        rp = ReportsParser(dmin=UTCDateTime(2012, 1, 1, 0, 0, 0),
                           dmax=UTCDateTime(2013, 11, 1, 0, 0, 0))
        # t = EventCA()
        t = EventSoCal()
        rp.sfilter = t.point_in_polygon

        for _f in fns:
            rp.read_reports(_f)

        correct = rp.get_correct(mmin=3.5, mmax=10.0)
        pid = correct[:, 0]
        ot = correct[:, 2].astype('float')
        lats = correct[:, 3].astype('float')
        lons = correct[:, 4].astype('float')
        deps = correct[:, 5].astype('float')
        mags = correct[:, 6].astype('float')
        ts1 = correct[:, 7].astype('float')
        lats1 = correct[:, 9].astype('float')
        lons1 = correct[:, 10].astype('float')
        mags1 = correct[:, 12].astype('float')
        rfns = correct[:, 21]
        diff = ts1 - ot
        magdiff = mags - mags1
        cnt = 0
        allcnt = 0
        allm = []
        dataX = []
        dataY = []
        popup_values = []

        fig = plt.figure()
        ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
        m = self.background_map(ax)
        cmap = cm.ScalarMappable(norm=Normalize(vmin=0, vmax=2), cmap='RdBu_r')
        stats_used = []
        for lon, lat, dep, delay, evid, lat1, lon1, dmag, time, mag, rfn in \
            zip(lons, lats, deps, diff, pid, lats1, lons1, magdiff, ot, mags, rfns):
            allcnt += 1
            try:
                if eventinfo is not None and len(eventinfo[evid]) != 4:
                    # print "Event %s does not have 4 initial picks." % evid
                    continue
            except KeyError:
                print "No event information available for: %s (%s)" % (
                    evid, UTCDateTime(time))
                continue
            if evid in self.event_excludes:
                print "Event %s was set to be excluded." % evid
                continue

            if computedelay:
                # Compute the expected alert time for the actual epicenter and
                # the first stations that detected the event
                class NetworkInfo:
                    def __init__(self):
                        self.networks = {
                            'ca': {
                                'lat': [],
                                'lon': [],
                                'chn': [],
                                'nw': [],
                                'nm': [],
                                'lc': [],
                                'color': 'black',
                                'label': 'UC Berkeley'
                            }
                        }

                    def get_networks(self):
                        return self.networks

                # read in SCEDC master station list
                fh = open(stationfn)
                scedc_stations = {}
                for _l in fh.readlines():
                    if _l.startswith('#'):
                        continue
                    net, sta, chan, loc, lt, ln, elev, ondate, offdate = _l.split(
                    )
                    ns = '.'.join((net, sta))
                    if ns not in scedc_stations:
                        scedc_stations[ns] = (float(lt), float(ln))
                ni = NetworkInfo()
                for _st in eventinfo[evid]:
                    ni.networks['ca']['lat'].append(scedc_stations[_st][0])
                    ni.networks['ca']['lon'].append(scedc_stations[_st][1])
                    ni.networks['ca']['nm'].append(_st)
                    if _st not in stats_used:
                        stats_used.append(_st)
                de = DelayEEW()
                elat, elon, edep, ttP, tstarget = \
                de.compute(ni, np.array([float(lon)]), np.array([float(lat)]),
                           np.array([float(dep)]),
                           vp=6.5, vs=3.5, nnst=4, procdelay=True, nmaps=500,
                           resultsfn=None, latencies=latencies)
                med = np.median(ttP)
                lb = scoreatpercentile(ttP, perc_min)
                ub = scoreatpercentile(ttP, perc_max)
            else:
                distance, index = tree.query(np.array([[lat, lon]]))
                irow, icol = divmod(index[0], lats_tt.shape[1])
                med = np.median(times[:, irow, icol])
                lb = scoreatpercentile(times[:, irow, icol], perc_min)
                ub = scoreatpercentile(times[:, irow, icol], perc_max)

            cnt += 1
            allm.append(mag)
            val = (delay - lb) / (ub - lb)
            print med, lb, ub, delay, val, med - delay
            vals.append(val)
            cl = cmap.to_rgba(val)
            x, y = m(lon, lat)
            dataX.append(x)
            dataY.append(y)
            info = '%s: %.2f %s\n' % (UTCDateTime(time), mag, evid)
            info += '%.2f %.2f %.2f\n' % (delay, med, val)
            for _st in eventinfo[evid]:
                info += ' %s' % _st
            popup_values.append(info)
            m.plot(x, y, ms=8, c=cl, marker='o', picker=5.)
            # plt.figure()
            # plt.hist(times[ilon, ilat, :], bins=np.arange(0, 30), normed=True, histtype='step')
            # plt.show()
        print "Stations used in detections:"
        print stats_used
        idx = np.where((np.array(vals) <= 1.0) & (np.array(vals) >= 0))
        print "%.1f lie within the %d and %d percentile" % (
            (idx[0].size / float(len(vals))) * 100, perc_min, perc_max)
        # plt.plot(allm, vals, 'bo')
        if interactive:
            self.popup(fig, dataX, dataY, popup_values)
        cax = fig.add_axes([0.87, 0.1, 0.05, 0.8])
        cb = ColorbarBase(cax, cmap='RdBu_r', norm=Normalize(vmin=0, vmax=2))
        cb.set_label('Alert accuracy')

        plt.figure()
        plt.hist(vals, bins=20)
        plt.show()
コード例 #22
0
            # Get start time as beginning of middle day
            if ifil == 1:
                refdate = st[1].stats.starttime
                tstart = datetime(refdate.year, refdate.month, refdate.day, 0,
                                  0, 0)
            else:
                tstart = (UTCDateTime(tstart) + 24 * 60 * 60).datetime
            jdaynum_sac[ifil] = refdate.julday

            # ensure start time in plot equals 0
            dt = st[0].stats.starttime.timestamp

            fig = plt.figure(figsize=(10, 8))
            colorday = np.arange(1, 5)
            norm = mcolors.Normalize(vmin=colorday.min(), vmax=colorday.max())
            cmap = cm.ScalarMappable(norm=norm, cmap=cm.rainbow)
            cmap.set_array([])
            t_zoom = 50
            for i in np.arange(0, 3):
                t = np.linspace(st[i].stats.starttime.timestamp - dt,
                                st[i].stats.endtime.timestamp - dt,
                                st[i].stats.npts)
                ax1 = fig.add_subplot(4, 1, 1)
                ax1.plot(t, st[i].data, color=cmap.to_rgba(i + 1))
                ax1.set(ylim=(3 * st[i].data.std() + st[i].data.mean(),
                              -3 * st[i].data.std() + st[i].data.mean()))
                ax1.set(xlim=(0, 3 * 24 * 60 * 60))
                ax1.set(title=sta + ' ' + ch + ' ' +
                        str(st[0].stats.starttime) + ' to ' +
                        str(st[2].stats.endtime) + ': total time shift = ' +
                        str(np.round(dt_totalshift_sec / 60 / 60, 3)) +
コード例 #23
0
ファイル: mpas_transect.py プロジェクト: trhille/MPAS-Tools
                 color=timeColors[timeSlice])

    speed_interpolant = LinearNDInterpolator(
        np.vstack((xCell, yCell)).T, speed[timeSlice, :])
    speed_transect = speed_interpolant(np.vstack((xArray, yArray)).T)

    speed_transect[speed_transect == 0] = np.nan
    speed_transect[thk_transect == 0.] = np.nan
    speedAx.plot((distance[-1] - distance) / 1000.,
                 speed_transect,
                 color=timeColors[timeSlice])

thickAx.plot((distance[-1] - distance) / 1000., bed_transect, color='black')

speedAx.set_xlabel('Distance (km)')
speedAx.set_ylabel('Surface\nspeed (m/yr)')
speedAx.set_xlim((0, 35))
thickAx.set_ylabel('Elevation\n(m asl)')
thickAx.set_xlim((0, 35))
transectFig.subplots_adjust(hspace=0.4)

cbar = plt.colorbar(cm.ScalarMappable(cmap='plasma'), ax=transectAx)
cbar.set_ticks(np.linspace(0, 1, 6))
cbar.set_label('Year', size='large')
years = str(time).lstrip('[').rstrip(']').split()
cbar.set_ticklabels(years[0::len(years) // 5])
cbar.ax.tick_params(labelsize=12)

plt.show()

dataset.close()
コード例 #24
0
    def draw_dendrogram(self, ax, pairs, values, labels, lw=20., alpha=0.4, cmap='viridis'):
        try:
            from matplotlib import collections as mc
            from matplotlib.pyplot import Arrow
            from matplotlib.pyplot import Normalize
            from matplotlib.pyplot import cm
        except ImportError:
            raise ImportError('You must install the matplotlib library to plot the minimum spanning tree.')

        min_index, max_index = min(pairs), max(pairs)
        if min_index < 0:
            raise ValueError('Indices should be non-negative')

        size = int(len(pairs) / 2 + 1)

        union_size = size
        if max_index > union_size - 1:
            union_size = max_index + 1
        union_size += 2

        # we will create Union Find as usual
        uf, sz = np.zeros(2 * union_size, dtype=int), np.ones(union_size)
        next_label = union_size + 1
        # also we need links
        l, r = np.arange(0, 2*union_size), np.arange(0, 2*union_size)

        next_label = union_size + 1
        for j in range(0, size - 1):
            a, b = pairs[2 * j], pairs[2 * j + 1]

            # we will stack first cluster on the left of second
            aa = a
            while aa != r[aa]:
                aa = r[aa]
            bb = b
            while bb != l[bb]:
                bb = l[bb]
            l[bb] = aa
            r[aa] = bb # linking


            aa = a
            while aa != l[aa]:
                aa = l[aa]
            bb = b
            while bb != r[bb]:
                bb = r[bb]
            l[next_label] = aa # marking the borders
            r[next_label] = bb

            aa, bb = self.fast_find(uf, a), self.fast_find(uf, b)
            uf[aa] = uf[bb] = next_label

            # i = next_label - union_size
            # a2 = (uf[a] != 0) * (aa - union_size)
            # b2 = (uf[b] != 0) * (bb - union_size)
            # na, nb = sz[a2], sz[b2]
            # sz[i] = na + nb

            next_label += 1

        x_arr = self.arrange_nodes_on_x_axis(uf, union_size, l, r, 200.)

        norm = len(np.unique(pairs))
        sm = cm.ScalarMappable(cmap=cmap,
                                   norm=Normalize(0, norm))
        sm.set_array(norm)

        colors = self.get_dendro_colors(labels)
        heights = {}
        uf.fill(0)
        next_label = union_size + 1
        for j in range(0, size - 1):
            v = np.log2(1. + values[j]) # logarithm
            heights[next_label] = v

            a, b = pairs[2 * j], pairs[2 * j + 1]

            # i = next_label - union_size
            aa, bb = self.fast_find(uf, a), self.fast_find(uf, b)
            x_arr[next_label] = (x_arr[r[aa]] + x_arr[l[bb]])/2.
            uf[aa] = uf[bb] = next_label
            next_label += 1

            # a = (uf[a] != 0) * (aa - union_size)
            # b = (uf[b] != 0) * (bb - union_size)
            # na, nb = sz[a], sz[b]
            # sz[i] = na + nb

            ha, hb = 0, 0
            xa, xb = x_arr[aa], x_arr[bb]
            if aa in heights:
                ha = heights[aa]
            if bb in heights:
                hb = heights[bb]

            c = 'gray'
            if labels[a] == labels[b] and labels[a] > 0:
                c = colors[labels[a]]

            ax.plot([xa, xa], [ha, v], color=c)
            ax.plot([xb, xb], [hb, v], color=c)
            ax.plot([xa, xb], [v, v], color=c)

        ax.set_xticks([])
        for side in ('right', 'top', 'bottom'):
            ax.spines[side].set_visible(False)
        ax.set_ylabel('distance')

        # line_collection.set_array(self._mst[:, 2].T)
        return ax
コード例 #25
0
    def alert_times_map(self,
                        fout=None,
                        m=None,
                        fig=None,
                        ax=None,
                        scale=10000.,
                        cb=False,
                        magerr=False,
                        interactive=False,
                        msscale=1,
                        realtime=False,
                        cmapname='jet'):
        cmap = cm.ScalarMappable(norm=Normalize(vmin=6, vmax=25),
                                 cmap=cmapname)
        if m is None and fig is None and ax is None:
            fig = plt.figure()
            ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
            m = self.background_map(ax)
        dataX = []
        dataY = []
        values = []
        for evid in self.del_coord.keys():
            lon, lat, delay, magdiff = self.del_coord[evid]
            x, y = m(lon, lat)
            dataX.append(x)
            dataY.append(y)
            values.append('%s: %.2f' % (evid, delay))
            cl = cmap.to_rgba(delay)
            if magerr:
                factor = math.sqrt(abs(float(magdiff)))
                sl2 = scale * factor
                p2 = Wedge((x, y),
                           sl2,
                           0,
                           360,
                           facecolor=cl,
                           edgecolor='black',
                           picker=5,
                           lw=1.0)
                ax.add_patch(p2)
            else:
                m.plot(x, y, ms=8 * msscale, c=cl, marker='s', picker=5.)
        print "Number of playback events plotted: %d" % len(
            self.del_coord.keys())

        if realtime:
            for evid in self.del_coord_rt.keys():
                lon, lat, delay, magdiff = self.del_coord_rt[evid]
                x, y = m(lon, lat)
                dataX.append(x)
                dataY.append(y)
                values.append('%s: %.2f' % (evid, delay))
                cl = cmap.to_rgba(delay)
                if magerr:
                    factor = math.sqrt(abs(float(magdiff)))
                    sl2 = scale * factor
                    p2 = Wedge((x, y),
                               sl2,
                               0,
                               360,
                               facecolor=cl,
                               edgecolor='black',
                               picker=5,
                               lw=1.0)
                    ax.add_patch(p2)
                else:
                    m.plot(x, y, ms=8 * msscale, c=cl, marker='o', picker=5.)
            print "Number of realtime events plotted: %d" % \
            len(self.del_coord_rt.keys())

        if interactive:
            try:
                # add a pop-up window showing the station and its value
                tooltip = wx.ToolTip(tip='')
                tooltip.Enable(False)
                tooltip.SetDelay(0)
                fig.canvas.SetToolTip(tooltip)

                def onMotion(event):
                    dir(event.artist)
                    line2d = event.artist
                    x = line2d.get_xdata()[0]
                    y = line2d.get_ydata()[0]
                    found = False
                    for i in xrange(len(dataX)):
                        radius = 5
                        if abs(x - dataX[i]) < radius and abs(
                                y - dataY[i]) < radius:
                            tip = '%s' % values[i]
                            tooltip.SetTip(tip)
                            tooltip.Enable(True)
                            found = True
                            break
                    if not found:
                        tooltip.Enable(False)

                fig.canvas.mpl_connect('pick_event', onMotion)
            except Exception, e:
                print "Cannot add wx.ToolTip: ", e
コード例 #26
0
ファイル: visualizer.py プロジェクト: Na-Z/3DMVGOD
def visualize_n_frustums_plus_ptclouds(p_planes_list, frustum_ptcloud):
    colors = vtk.vtkNamedColors()

    # set a renderer and a render window
    renderer = vtk.vtkRenderer()
    renderer.SetBackground(colors.GetColor3d("Silver"))
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetSize(600, 600)
    renderWindow.SetWindowName("Frustum Intersection")
    renderWindow.AddRenderer(renderer)

    # an interactor
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetInteractorStyle(
        vtk.vtkInteractorStyleTrackballCamera())
    renderWindowInteractor.SetRenderWindow(renderWindow)

    # add n frustums
    for i, p_planes in enumerate(p_planes_list):

        planesArray = list(p_planes.flatten())
        planes = vtk.vtkPlanes()
        planes.SetFrustumPlanes(planesArray)

        frustum = vtk.vtkFrustumSource()
        frustum.ShowLinesOff()
        frustum.SetPlanes(planes)

        shrink = vtk.vtkShrinkPolyData()
        shrink.SetInputConnection(frustum.GetOutputPort())
        shrink.SetShrinkFactor(1.)

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(shrink.GetOutputPort())

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().EdgeVisibilityOn()
        actor.GetProperty().SetColor(colors.GetColor3d("Banana"))
        actor.GetProperty().SetOpacity(.2)

        renderer.AddActor(actor)

    # add ptclouds
    # assin color to ptcloud (n,4)
    cm = colormap.get_cmap('RdBu')
    color_mapper = colormap.ScalarMappable(norm=matplotlib.colors.Normalize(
        vmin=0, vmax=1, clip=True),
                                           cmap=cm)
    color = color_mapper.to_rgba(frustum_ptcloud[:, 3])[:, :3] * 255
    ptcloud = np.hstack((frustum_ptcloud[:, 0:3], color))

    # Create the geometry of a point (the coordinate)
    points = vtk.vtkPoints()
    # Create the topology of the point (a vertex)
    vertices = vtk.vtkCellArray()
    # Setup colors
    Colors = vtk.vtkUnsignedCharArray()
    Colors.SetNumberOfComponents(3)
    Colors.SetName("Colors")
    # Add points
    for i in range(0, len(ptcloud)):
        p = ptcloud[i, :3]
        id = points.InsertNextPoint(p)
        vertices.InsertNextCell(1)
        vertices.InsertCellPoint(id)
        Colors.InsertNextTuple3(ptcloud[i, 3], ptcloud[i, 4], ptcloud[i, 5])
    point = vtk.vtkPolyData()
    # Set the points and vertices we created as the geometry and topology of the polydata
    point.SetPoints(points)
    point.SetVerts(vertices)
    point.GetPointData().SetScalars(Colors)
    point.Modified()

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(point)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().EdgeVisibilityOn()
    actor.GetProperty().SetColor(colors.GetColor3d("Tomato"))
    actor.GetProperty().SetOpacity(.3)

    renderer.AddActor(actor)

    transform = vtk.vtkTransform()
    transform.Translate(0, 0, 0)
    axes = vtk.vtkAxesActor()
    #  The axes are positioned with a user transform
    axes.SetAxisLabels(1)
    axes.SetUserTransform(transform)
    axes.AxisLabelsOff()
    axes.SetTotalLength(3.0, 3.0, 3.0)
    renderer.AddActor(axes)

    renderer.SetUseDepthPeeling(1)
    renderer.SetOcclusionRatio(0.1)
    renderer.SetMaximumNumberOfPeels(100)
    renderWindow.SetMultiSamples(0)
    renderWindow.SetAlphaBitPlanes(1)

    # Position the camera so that we can see the frustum
    renderer.GetActiveCamera().SetPosition(1, 0, 0)
    renderer.GetActiveCamera().SetFocalPoint(0, 0, 0)
    renderer.GetActiveCamera().SetViewUp(0, 1, 0)
    renderer.GetActiveCamera().Azimuth(30)
    renderer.GetActiveCamera().Elevation(30)
    renderer.ResetCamera()

    # render an image (lights and cameras are created automatically)
    renderWindow.Render()

    # begin mouse interaction
    renderWindowInteractor.Start()
コード例 #27
0
ファイル: live_histo_gauss.py プロジェクト: AndiZm/curcor
def main():

    Figure1 = plt.figure(num=None, figsize=(14, 7))
    ax1 = Figure1.add_subplot(1, 1, 1)  # subplots

    #### files einlesen ####
    T = {}  # dictionary wie liste

    path = '20200120_PMT1_newnewbase_newamp/'

    for file in os.listdir(path):
        if not file.endswith('.txt'):
            continue

        fn = path + file
        z1, z2 = file.split('.txt')[0].split('_')
        z1, z2 = int(z1), int(z2)

        if not z1 in T.keys(
        ):  # liste von allen ersten 'Zahlen' in dictionary also keys z1, value = z2 wert in keys (1,2,3)
            T[z1] = {}  # leeres Unterdictionary um anzufangen

        T[z1][z2] = np.genfromtxt(fn)  # fuegt z2 in unterdictionary

    #### colormaps ####

    cm_sub = np.linspace(1.0, 0.0, len(T))  #len(T)) oder 4
    colors = [cm.viridis(x) for x in cm_sub]

    transmission = [
        0.0001, 0.0003, 0.0005, 0.0007, 0.0012, 0.0014, 0.0016, 0.0019, 0.003,
        0.0042, 0.004, 0.0042, 0.008, 0.006, 0.012, 0.016, 0.022, 0.023, 0.029,
        0.037, 0.067, 0.072, 0.09, 0.16, 0.23, 0.54
    ]
    rate = [
        1.15, 1.23, 1.37, 1.6, 1.98, 2.02, 2.26, 2.58, 3.43, 4.03, 3.99, 4.73,
        7.31, 5.54, 9.58, 12.42, 16.33, 17.18, 21.17, 25.95, 47.47, 55.96,
        62.49, 101.39, 127.40
    ]

    print(len(T))
    x = []
    y = []
    clabels = []
    V = [0, 1150, 1250, 1350]
    d_rate = []
    int_rate = []
    mu1 = []
    d_mu1 = []
    mu2 = []
    d_mu2 = []
    sig1 = []
    d_sig1 = []
    distance = []
    d_distance = []
    ylabels = []
    sig2 = []
    d_sig2 = []
    mean = []
    peakvalley = []

    for j, i in enumerate(sorted(
            T.keys())):  #range(1,4):      enumerate(sorted(T.keys())):
        if i in [47, 49, 53, 56, 60]:
            x.append(T[i][1][:, 0] - 127)
        else:
            x.append(T[i][1][:, 0])
        y.append(T[i][1][:, 1])
        ax1.plot(x[-1], y[-1] / 0.8, color=colors[j])  #[i-1]  #, label=V[i])
        clabels.append(i)

        #### Gauss fit for data and noise ####
        if 0 <= j <= 7:  #fuer 2 ->11
            m = -6
            mask1 = (
                x[-1] <= -6.
            )  # fuer 2 -> 10                                           # Bereich aendern
            x_fit1 = x[-1][mask1]
            y_fit1 = y[-1][mask1]

            p01 = [1e7, -10, 10]  # Startparameter aendern
            popt1, pcov1 = curve_fit(gauss_function,
                                     x_fit1,
                                     y_fit1 / 0.8,
                                     p0=p01,
                                     maxfev=1000000)
            #print("Mittel:\t" + str(j) + '   ' + str(i) + '    ' + str(popt[1]))

            mu1.append(popt1[1])
            perr1 = np.sqrt(np.diag(pcov1))
            d_mu1.append(perr1[1])

            sig1.append(popt1[2])
            d_sig1.append(perr1[2])

            peakvalley.append(np.abs(mu1[-1]) - np.abs(m))

            # ax1.plot(x_fit1, gauss_function(x_fit1,*popt1), c='black', zorder=20)
            # ax1.plot(mu1[-1], gauss_function(mu1[-1], *popt1), marker='o',c='black')

            int_rate.append(popt1[0] * np.abs(popt1[2]) * np.sqrt(2 * np.pi) *
                            1e-6)
            d_rate.append(
                np.sqrt((np.sqrt(2 * np.pi) * popt1[2] * perr1[0])**2 +
                        (np.sqrt(2 * np.pi) * popt1[0] * perr1[2])**2) * 1e-6)

            print "Integrierte Rate:\t %d %d %.2f  MHz  Fehler Rate:\t %.2f MHz" % (
                j, i, int_rate[j], d_rate[j])
            print "Mittel:\t %d %d %.2f   Fehler:\t %.2f MHz" % (j, i, mu1[j],
                                                                 d_mu1[j])
            print "Breite:\t %d %d %.2f   Fehler:\t %.2f MHz" % (j, i, sig1[j],
                                                                 d_sig1[j])
            print "Peak-Valley:\t %d %d %.2f " % (j, i, peakvalley[j])

            ylabels.append(round(int_rate[-1], 2))

            #### noise gauss fit ####
            x_plot = np.arange(-15, 10, 0.01)
            mask2 = (x[-1] <= 10) & (x[-1] >= -5)
            x_fit2 = x[-1][mask2]
            y_fit2 = y[-1][mask2]

            p02 = [1.6e8, 0.0, 1.1]
            popt2, pcov2 = curve_fit(
                gauss_function, x_fit2, y_fit2 / 0.8, p0=p02
            )  #, bounds= ([1e5, -10, 0.1], [1e9, 5, 10]), maxfev = 1000000)
            # print("Mittel:\t" + str(j) + '   ' + str(i) + '    ' + str(popt2[1]))

            mu2.append(popt2[1])
            perr2 = np.sqrt(np.diag(pcov2))
            d_mu2.append(perr2[1])

            sig2.append(popt2[2])
            d_sig2.append(perr2[2])

            # ax1.plot(x_plot, gauss_function(x_plot, *popt2), c='black', zorder=20)

            distance.append(mu1[-1] - mu2[-1])
            d_distance.append(d_mu1[-1] - d_mu2[-1])

            print "Noise Mittel:\t %d %d %.2f   Fehler:\t %.2f MHz" % (
                j, i, mu2[j], d_mu2[j])
            print "Noise Breite:\t %d %d %.2f   Fehler:\t %.2f MHz" % (
                j, i, sig2[j], d_sig2[j])

        if 8 <= j <= 16:  # fuer 2 bis 14                      # Zahl aendern
            mask1 = (x[-1] <= -6.)  # fuer 2 -10        # Bereich aendern
            x_fit1 = x[-1][mask1]
            y_fit1 = y[-1][mask1]

            p01 = [1e6, -10, 10]  # Startparameter aendern
            popt1, pcov1 = curve_fit(gauss_function,
                                     x_fit1,
                                     y_fit1 / 0.8,
                                     p0=p01,
                                     maxfev=1000000)
            #print(popt, pcov)
            # print("Mittel:\t" + str(j) + '   ' + str(i) + '    ' + str(popt[1]))

            mu1.append(popt1[1])
            perr1 = np.sqrt(np.diag(pcov1))
            d_mu1.append(perr1[1])

            sig1.append(popt1[2])
            d_sig1.append(perr1[2])

            peakvalley.append(np.abs(mu1[-1]) - np.abs(m))

            # ax1.plot(x_fit1, gauss_function(x_fit1,*popt1), c='black', zorder=20)
            # ax1.plot(mu1[-1], gauss_function(mu1[-1], *popt1), marker='o',c='black')
            #except:
            #print('Fit failed')
            #plt.plot(x_fit, gauss_function(x_fit,*p0), c='black')

            int_rate.append(popt1[0] * np.abs(popt1[2]) * np.sqrt(2 * np.pi) *
                            1e-6)
            d_rate.append(
                np.sqrt((np.sqrt(2 * np.pi) * popt1[2] * perr1[0])**2 +
                        (np.sqrt(2 * np.pi) * popt1[0] * perr1[2])**2) * 1e-6)

            print "Integrierte Rate:\t %d %d %.2f  MHz  Fehler Rate:\t %.2f MHz" % (
                j, i, int_rate[j], d_rate[j])
            print "Mittel:\t %d %d %.2f   Fehler:\t %.2f MHz" % (j, i, mu1[j],
                                                                 d_mu1[j])
            print "Breite:\t %d %d %.2f   Fehler:\t %.2f MHz" % (j, i, sig1[j],
                                                                 d_sig1[j])
            print "Peak-Valley:\t %d %d %.2f " % (j, i, peakvalley[j])

            ylabels.append(round(int_rate[-1], 2))

            #### noise gauss fit ####
            x_plot = np.arange(-15, 15, 0.01)
            mask2 = (x[-1] <= 10) & (x[-1] >= -5)
            x_fit2 = x[-1][mask2]
            y_fit2 = y[-1][mask2]

            p02 = [1.6e8, -2, 1.1]
            popt2, pcov2 = curve_fit(
                gauss_function, x_fit2, y_fit2 / 0.8, p0=p02
            )  #, bounds= ([1e5, -10, 0.1], [1e9, 5, 10]), maxfev = 1000000)
            #print("Mittel:\t" + str(j) + '   ' + str(i) + '    ' + str(popt[1]))

            mu2.append(popt2[1])
            perr2 = np.sqrt(np.diag(pcov2))
            d_mu2.append(perr2[1])

            sig2.append(popt2[2])
            d_sig2.append(perr2[2])

            # ax1.plot(x_plot, gauss_function(x_plot, *popt2), c='black', zorder=20)

            distance.append(mu1[-1] - mu2[-1])
            d_distance.append(d_mu1[-1] - d_mu2[-1])

        if 17 <= j <= 18:  # Zahl aendern
            mask1 = (x[-1] <= -10.)  #fuer 1 -10        # Bereich aendern
            x_fit1 = x[-1][mask1]
            y_fit1 = y[-1][mask1]

            p01 = [1e6, -20, 10]  # Startparameter aendern
            popt1, pcov1 = curve_fit(gauss_function,
                                     x_fit1,
                                     y_fit1 / 0.8,
                                     p0=p01,
                                     maxfev=1000000)
            #print(popt, pcov)
            # print("Mittel:\t" + str(j) + '   ' + str(i) + '    ' + str(popt[1]))

            mu1.append(popt1[1])
            perr1 = np.sqrt(np.diag(pcov1))
            d_mu1.append(perr1[1])

            sig1.append(popt1[2])
            d_sig1.append(perr1[2])

            peakvalley.append(np.abs(mu1[-1]) - np.abs(m))

            # ax1.plot(x_fit1, gauss_function(x_fit1,*popt1), c='black', zorder=20)
            # ax1.plot(mu1[-1], gauss_function(mu1[-1], *popt1), marker='o',c='black')
            #except:
            #print('Fit failed')
            #plt.plot(x_fit, gauss_function(x_fit,*p0), c='black')

            int_rate.append(popt1[0] * np.abs(popt1[2]) * np.sqrt(2 * np.pi) *
                            1e-6)
            d_rate.append(
                np.sqrt((np.sqrt(2 * np.pi) * popt1[2] * perr1[0])**2 +
                        (np.sqrt(2 * np.pi) * popt1[0] * perr1[2])**2) * 1e-6)

            print "Integrierte Rate:\t %d %d %.2f  MHz  Fehler Rate:\t %.2f MHz" % (
                j, i, int_rate[j], d_rate[j])
            print "Mittel:\t %d %d %.2f   Fehler:\t %.2f MHz" % (j, i, mu1[j],
                                                                 d_mu1[j])
            print "Breite:\t %d %d %.2f   Fehler:\t %.2f MHz" % (j, i, sig1[j],
                                                                 d_sig1[j])
            print "Peak-Valley:\t %d %d %.2f " % (j, i, peakvalley[j])

            ylabels.append(round(int_rate[-1], 2))

            #### noise gauss fit ####
            x_plot = np.arange(-15, 15, 0.01)
            mask2 = (x[-1] <= 10) & (x[-1] >= -7)
            x_fit2 = x[-1][mask2]
            y_fit2 = y[-1][mask2]

            p02 = [1.6e8, -2, 1.1]
            popt2, pcov2 = curve_fit(
                gauss_function, x_fit2, y_fit2 / 0.8, p0=p02
            )  #, bounds= ([1e5, -10, 0.1], [1e9, 5, 10]), maxfev = 1000000)
            #print("Mittel:\t" + str(j) + '   ' + str(i) + '    ' + str(popt[1]))

            mu2.append(popt2[1])
            perr2 = np.sqrt(np.diag(pcov2))
            d_mu2.append(perr2[1])

            sig2.append(popt2[2])
            d_sig2.append(perr2[2])

            # ax1.plot(x_plot, gauss_function(x_plot, *popt2), c='black', zorder=20)

            distance.append(mu1[-1] - mu2[-1])
            d_distance.append(d_mu1[-1] - d_mu2[-1])

    print(len(mu1))
    print(len(mu2))
    print(len(d_mu2))
    print(len(transmission[0:19]))
    print(len(int_rate))
    print(len(d_rate))
    print(len(sig1))
    print(len(d_sig1))
    print(len(sig2))
    print(mu1[0])
    print(sig2[0])

    #### extrapolation trans ####

    z1 = np.polyfit(transmission[0:19], int_rate, 1)
    p1 = np.poly1d(z1)

    ####  Colorbar ####

    norm = mpl.colors.Normalize(vmin=min(clabels), vmax=max(clabels))
    #colormap_r = mpl.colors.ListedColormap(col1[::-1])
    cmap = cm.ScalarMappable(norm=norm, cmap=cm.viridis_r)
    cmap.set_array([])
    cbar = Figure1.colorbar(cmap,
                            ticks=[3, 9, 15, 20, 26, 32, 37, 42],
                            format='%d')
    cbar.ax.set_yticklabels([
        ylabels[0], ylabels[2], ylabels[5], ylabels[8], ylabels[11],
        ylabels[14], ylabels[16], ylabels[18]
    ])
    cbar.set_label('Incident rate (MHz)', size=19, labelpad=17)
    plt.setp(cbar.ax.yaxis.get_ticklabels(), fontsize=20)

    #### Plotting ####
    path2 = path + 'PlotsArbeit/'

    func.custom_plot(ax1,
                     Figure1,
                     path2 + 'Histo_PMT1newbase_1100_log',
                     '',
                     'Pulse height',
                     'Count rate (MHz)',
                     xlim=[10, -120],
                     ylim=[1.e0, 1.e9],
                     sciy='log')

    Figure2 = plt.figure(num=None, figsize=(14, 7))
    ax2 = Figure2.add_subplot(1, 1, 1)
    ax2.errorbar(transmission[0:19],
                 int_rate,
                 yerr=d_rate,
                 c='blue',
                 marker='.',
                 linestyle='')
    #ax2.plot(transmission[0:19], p1(transmission[0:19]))
    func.custom_plot(
        ax2, Figure2, path2 + 'rates_vs_trans', '', 'Transmission',
        'Rate (MHz)'
    )  #, [0,0.03], [0,150])           #[0.0, 0.03], [0.0, 170])

    Figure3 = plt.figure(num=None, figsize=(14, 7))
    ax3 = Figure3.add_subplot(1, 1, 1)
    ax3.errorbar(ylabels, mu1, yerr=d_mu1, c='blue', marker='.', linestyle='')
    ax30 = ax3.twinx()
    # make a plot with different y-axis using second axis object
    ax30.set_ylabel("Pulse height (mV)", fontsize=19, labelpad=9)
    mn30, mx30 = ax3.get_ylim()
    ax30.set_ylim((mn30 * 200) / 127, (mx30 * 200) / 127)
    ax30.tick_params(axis='y', labelsize=13)
    func.custom_plot(
        ax3, Figure3, path2 + 'photonpeak_vs_rate', '', 'Rate (MHz)',
        'Pulse height (ADC)'
    )  #, [0,150], [-20,-11.5])         #[0.0, 170], [-35, -20])

    Figure4 = plt.figure(num=None, figsize=(14, 7))
    ax4 = Figure4.add_subplot(1, 1, 1)
    ax4.errorbar(ylabels,
                 np.abs(sig1),
                 yerr=d_sig1,
                 c='blue',
                 marker='.',
                 linestyle='')
    ax40 = ax4.twinx()
    # make a plot with different y-axis using second axis object
    ax40.set_ylabel("Pulse height (mV)", fontsize=19, labelpad=9)
    mn40, mx40 = ax4.get_ylim()
    ax40.set_ylim((mn40 * 200) / 127, (mx40 * 200) / 127)
    ax40.tick_params(axis='y', labelsize=13)
    func.custom_plot(
        ax4, Figure4, path2 + 'Sigma_photonpeak_vs_rate', '', 'Rate (MHz)',
        'Pulse height (ADC)')  #, [0,150], [4,12])    #[0.0, 150], [6, 20])

    Figure5 = plt.figure(num=None, figsize=(14, 7))
    ax5 = Figure5.add_subplot(1, 1, 1)
    ax5.errorbar(ylabels, mu2, yerr=d_mu2, c='blue', marker='.', linestyle='')
    func.custom_plot(
        ax5, Figure5, path2 + 'noisepeak_vs_rate', '', 'Rate (MHz)',
        'Pulse height (ADC)')  #, [0,150], [-5,1])     #[0.0, 170], [-7., 1.])

    plt.show()
コード例 #28
0
img = make_brain_image(views, brains[-1], text="C", text_loc="lup", text_pan=0)
ax = axes["C"]
ax.imshow(img)

# visselten
param_n = params_n[:, 2]
rgba = params_to_rgba(param_n)
brains.append(
    plot_rgba(rgba, labels, parc, figsize=figsize, background=background))
img = make_brain_image(views, brains[-1], text="D", text_loc="lup", text_pan=0)
ax = axes["D"]
ax.imshow(img)

ax = axes["E"]
norm = Normalize(vmin, vmax)
scalmap = cm.ScalarMappable(norm, cmap)
plt.colorbar(scalmap, cax=ax, orientation="horizontal")
ax.tick_params(labelsize=fontsize)
ax.set_xlabel("DICS power (NAI normalised)", fontsize=fontsize * 0.8)
plt.suptitle("Estimated power change from resting state", fontsize=fontsize)
plt.savefig("../images/dics_figure.png")
# # rainbow
# this_rgba = np.zeros((len(labels), 4))
# params = np.abs(aic_comps["sig_params"])
# param_norms = np.linalg.norm(params, axis=1)
# for idx in range(len(params)):
#     if param_norms[idx]:
#         params[idx,] /= param_norms[idx]
# this_rgba[:,:3] = params
# param_norms = param_norm(param_norms)
# this_rgba[:,3] = param_norms
コード例 #29
0
def add_rgb_dimention(array):
    scaler_map = cm.ScalarMappable(cmap="Oranges")
    array = scaler_map.to_rgba(array)[:, :-1]
    return array
コード例 #30
0
import pandas as pd
# %matplotlib inline
# %% codecell

# Load DataFrame
station = 'CC01'
path2pkl = "./pkls/"

df = pd.read_pickle(path2pkl+station+'_data.pkl')
df
# %% codecell
# loop through channels
fig = plt.figure(figsize=(10, 12))
colorday = np.arange(0, 5)
norm = mcolors.Normalize(vmin=colorday.min(), vmax=colorday.max())
cmap = cm.ScalarMappable(norm=norm, cmap=cm.Set3)
cmap.set_array([])
icount = 0
FS = 13
cmap.to_rgba(4)
for ch, data in df.iteritems():
    ax1 = fig.add_subplot(3, 1, 1)
    ax1.plot(data['jdaynum_mseed_ch'], data['nsec_mseed_actual_ch']/60/60, color=cmap.to_rgba(icount), label=ch, linewidth=4-icount/2)
    # ax1.set_xlabel('Jday', fontsize=FS)
    ax1.set_ylabel('Mseed file length (hours)', fontsize=FS)
    ax1.tick_params(labelsize=FS)
    ax1.grid(True)

    ax2 = fig.add_subplot(3, 1, 2)
    ax2.plot(data['jdaynum_mseed_ch'], data['dt_end_gap_sec_ch'], color=cmap.to_rgba(icount), label=ch, linewidth=4-icount/2)
    # ax2.set_xlabel('Jday', fontsize=FS)