Пример #1
0
def plot_AGonia_boxes(data_path, Score, box_idx):
    '''Function used as input for the dynamic map in function boxes_exploration():
    holoviews based plot to see Agonia boxes on top of median projection and mean trace
    for selected boxes.

    Parameters
    ----------
    data_path : string
        path to folder containing the data
    Score : float
        threshold for detection confidence for each box
    box_idx : int
        index of box to plot trace
    Returns
    -------
    Holoviews image to feed to dynamic map, selected cell is indicated with green square'''

    data_name, median_projection, boxes_path = get_files_names(data_path)
    #Yr, dims, T = cm.load_memmap(fname_new)
    #images = np.reshape(Yr.T, [T] + list(dims), order='F')
    images = io.imread(os.path.join(data_path, data_name[0]))
    with open(boxes_path, 'rb') as f:
        boxes = pickle.load(f)
        f.close()
    boxes = boxes[boxes[:, 4] > Score].astype('int')
    roi_bounds = hv.Path([
        hv.Bounds(
            tuple([
                roi[0], median_projection.shape[0] - roi[1], roi[2],
                median_projection.shape[0] - roi[3]
            ])) for roi in boxes[:, :4]
    ]).options(color='red')
    img = hv.Image(median_projection,
                   bounds=(0, 0, median_projection.shape[1],
                           median_projection.shape[0])).options(cmap='gray')

    #box_trace = images[:,boxes[box_idx,1]:boxes[box_idx,3],boxes[box_idx,0]:boxes[box_idx,2]].mean(axis=(1,2))
    ola = Extractor()
    for frame in images:
        ola.extract(frame, [boxes[box_idx]])
    box_trace = ola.get_traces().squeeze()
    box_square = hv.Path([
        hv.Bounds(
            tuple([
                boxes[box_idx,
                      0], median_projection.shape[0] - boxes[box_idx, 1],
                boxes[box_idx,
                      2], median_projection.shape[0] - boxes[box_idx, 3]
            ]))
    ]).options(color='lime')
    return ((img * roi_bounds * box_square).opts(width=600, height=600) +
            hv.Curve(
                (np.linspace(0,
                             len(box_trace) - 1, len(box_trace)), box_trace),
                'Frame', 'Mean box Fluorescence').opts(width=600,
                                                       framewise=True)).cols(1)
Пример #2
0
def hv_paths(a, b, c, d):
    diag_1 = hv.Path([np.c_[loop.values[a, :],
                            loop.values[c, :]].T]).options(opts_path_1)
    diag_2 = hv.Path([np.c_[loop.values[b, :],
                            loop.values[d, :]].T]).options(opts_path_2)
    point_1 = ((loop.values[a, :] + loop.values[c, :]) / 2)
    point_2 = ((loop.values[b, :] + loop.values[d, :]) / 2)
    diag_1_point = hv.Points((point_1[0], point_1[1])).options(opts_point_1)
    diag_2_point = hv.Points((point_2[0], point_2[1])).options(opts_point_2)
    return diag_1 * diag_2 * diag_1_point * diag_2_point
Пример #3
0
    def plot(self):
        import holoviews as hv
        if not self.data:
            return hv.Scatter([]) * hv.Path([])

        if not self.vdim > 1:
            return hv.Scatter(self.data) * hv.Path([])
        else:
            xs = list(self.data.keys())
            ys = list(self.data.values())
            return hv.Path((xs, ys)) * hv.Scatter([])
Пример #4
0
def coastlines(resolution='110m', lon_360=False):
    """ A custom method to plot in cylyndrical equi projection, most useful for
        native projections, geoviews currently supports only Web Mercator in
        bokeh mode.
        
        Other resolutions can be 50m
        
        lon_360 flag specifies if longitudes are from -180 to 180 (default) or 0 to 360
        TODO: if hv.Polygons is used instead of overlay it is way faster but 
              something is wrong there.
    """
    try:
        import cartopy.io.shapereader as shapereader
        from cartopy.io.shapereader import natural_earth
        import shapefile
        filename = natural_earth(resolution=resolution,
                                 category='physical',
                                 name='coastline')

        sf = shapefile.Reader(filename)
        fields = [x[0] for x in sf.fields][1:]
        records = sf.records()
        shps = [s.points for s in sf.shapes()]
        pls = []
        for shp in shps:
            lons = [lo for lo, _ in shp]
            lats = [la for _, la in shp]
            if lon_360:
                lats_patch1 = [lat for lon, lat in zip(lons, lats) if lon < 0]
                lons_patch1 = [lon + 360.0 for lon in lons if lon < 0]
                if any(lons_patch1):
                    pls.append(
                        hv.Path((lons_patch1, lats_patch1))(style={
                            'color': 'Black'
                        }))
                lats_patch2 = [
                    lat if lon >= 0 else None for lon, lat in zip(lons, lats)
                ]
                lons_patch2 = [lon if lon >= 0 else None for lon in lons]
                if any(lons_patch2):
                    pls.append(
                        hv.Path((lons_patch2, lats_patch2))(style={
                            'color': 'Black'
                        }))
            else:
                pls.append(hv.Path((lons, lats))(style={'color': 'Black'}))
        return hv.Overlay(pls)
    except Exception as err:
        print(
            'Overlaying Coastlines not available from holoviews because: {0}'.
            format(err))
Пример #5
0
def make_circle(radius=DEFAULT_RADIUS):
    def circle(t):
        return (radius * np.sin(t), radius * np.cos(t), t)

    lin = np.linspace(-np.pi, np.pi, 200)
    return hv.Path([circle(lin)]).opts(img_opts).opts(line_width=2,
                                                      color='red')
Пример #6
0
    def histogram_plot(self, xtrack=None, atrack=None, x=None, y=None):
        """plot histogram at xtrack, atrack"""

        if x is not None:
            xtrack = x
        if y is not None:
            atrack = y

        # atrack and xtrack are from mouse or user. get the nearest where histogram is defined
        xtrack, atrack = self._get_xatrack(xtrack=xtrack, atrack=atrack)

        # get histogram
        hist_at = self.gradients_hist.sel(atrack=atrack,
                                          xtrack=xtrack,
                                          method='nearest',
                                          tolerance=500)

        hp_list = []
        for sel_one2D in self.combine_all:
            hist2D_at = hist_at.sel(sel_one2D)
            hist2D360 = circ_hist(hist2D_at['weight'])
            style = self._get_style(hist2D_at)
            hp_list.append(
                hv.Path(hist2D360, kdims=['xtrack_g',
                                          'atrack_g']).opts(axiswise=False,
                                                            framewise=False,
                                                            aspect='equal',
                                                            **style))

        return hv.Overlay(hp_list).opts(xlabel='xtrack %d' % xtrack,
                                        ylabel='atrack %d' % atrack,
                                        width=200,
                                        height=200)
Пример #7
0
def U_plt(current_dataset,ssha_dataset,title,bounds=(-166,13.5,-154, 25)):
    """
    plot current velocities magntiudes averaged from 2008 to 2016 and Jason-2 tracks from dataset
    """
    # plot coastline
    coast = gf.coastline.geoms('50m', bounds=bounds).opts(color='black',
                                                                        xticks=5,
                                                                        yticks=5,
                                                                        yformatter='%f °N',
                                                                        xformatter = '%f °E') 
    # plot surface velocity magnitudes
    lats = current_dataset['lat'].reshape(len(current_dataset['lat']))
    _lons = current_dataset['lon'].reshape(len(current_dataset['lon']))
    lons = (_lons % 180) - (_lons >= 180) * 180
                                 
    u = np.mean(current_dataset['u'],axis=0)
    v = np.mean(current_dataset['v'],axis=0)
    magnitude = np.sqrt(u ** 2 + v ** 2)
    vel = hv.Image((lons,lats,magnitude)).opts(colorbar=True,
                                             cmap ='viridis',
                                             clabel='m/s',
                                             projection=ccrs.PlateCarree(),
                                             tools=['hover'])
    # plot tracks
    lon_range = (np.array(ssha_dataset['lon range']) % 180) - (np.array(ssha_dataset['lon range']) >= 180) * 180
    lat_range = ssha_dataset['lat range']
    track = hv.Path((lon_range ,lat_range)).opts(color = 'black')
    
    return (coast * vel * track).opts(projection=ccrs.PlateCarree(),
                                 title = title,
                                 height=300,
                                 width=500)
Пример #8
0
def ngon_ring(x=0,
              y=0,
              specs=[(1, 1), (.9, .9)],
              arc=(0, 2 * np.pi),
              orientation=0,
              npoints=40,
              line_color='k',
              line_alpha=1,
              fill_color='k',
              fill_alpha=0.,
              label=''):
    ''' an ngon outline with finite width
    '''
    l = ngon_data(x,
                  y,
                  specs=specs,
                  arc=arc,
                  orientation=orientation,
                  npoints=npoints)
    d = (np.hstack([l[0][:, 0],
                    l[1][::-1, 0]]), np.hstack([l[0][:, 1], l[1][::-1, 1]]))

    o_b = {
        'Polygons': dict(color=fill_color, alpha=fill_alpha, line_alpha=0),
        'Path': dict(line_color=line_color, line_alpha=line_alpha)
    }
    o_m = {
        'Polygons': dict(color=fill_color, alpha=fill_alpha),
        'Path': dict(color=line_color, alpha=line_alpha)
    }
    return (hv.Polygons([d], label=label) * hv.Path([l[0], l[1]])).opts(
        o_b, backend='bokeh')  #.opts( o_m, backend='matplotlib ')
Пример #9
0
def execute(csvfilepath):
    #df = pd.read_csv('Dataset/desktop_tracks.csv')
    df = pd.read_csv(csvfilepath)
    print(max(df['Mean']))
    tracks = [
        df.loc[df['Mean'] == x, ['Slice', 'X', 'Y', 'Mean']]
        for x in range(1, int(max(df['Mean'])))
    ]
    print("Number of Tracks:", len(tracks))
    tracklists = [
        list(zip(tracks[x]['X'], tracks[x]['Y'], tracks[x]['Slice']))
        for x in range(len(tracks))
    ]
    tracklists[0:3]
    trackplot = hv.Path(tracklists, vdims='time')
    trackplot.opts(cmap='Inferno', color='time', line_width=2)

    outfilename = "trackplot.html"
    hv.save(trackplot, outfilename, fmt='html')

    #file_name = "hello_world.txt"
    #copyfile(input, file_name)
    #f = open(file_name, "w")
    #f.write("Hello, World!")
    #f.close()

    return {'Output': outfilename}
Пример #10
0
 def tunes(bet_x, bet_y):
     bet_x -= math.floor(bet_x)
     bet_y -= math.floor(bet_y)
     color = '#30d5c8'
     path = hv.Path([[(bet_x - 0.05, bet_y), (bet_x + 0.05, bet_y)],
                     [(bet_x, bet_y - 0.05), (bet_x, bet_y + 0.05)]])
     path.opts(color=color, line_width=4)
     return path
Пример #11
0
def plot_surface(obj, **args):
    region = args.get('region', None)
    idx = obj.tag2idx(region)
    tags = args.get('tags', False)
    coord = args.get('coord', None)
    locator = args.get('locator', False)
    fill = args.get('fill', None)

    amin = np.min(obj.bbox_min[idx], axis=0)
    amax = np.max(obj.bbox_max[idx], axis=0)
    wh = amax - amin
    if np.min(wh) < 250:
        wh = wh / np.min(wh) * 250
    hvobj = hv.Path([obj.boundary[i]
                     for i in idx]).opts(style=dict(color='k'),
                                         plot=dict(yaxis=None,
                                                   xaxis=None,
                                                   aspect='equal',
                                                   width=int(ceil(wh[0])),
                                                   height=int(ceil(wh[1]))))

    if fill is not None:
        vals = np.nan * np.zeros((obj.num, ))
        for k in fill.keys():
            _idx = obj.tag2idx(k)
            for i in _idx:
                vals[i] = fill[k]

        imin = np.min(obj.bbox_min[idx], axis=0).astype(int)
        imax = np.max(obj.bbox_max[idx], axis=0).astype(int)
        im = np.nan * np.zeros(tuple((imax - imin + 1)[::-1].tolist()))
        for i in idx:
            im[obj._coords[i][:, 1] - imin[1],
               obj._coords[i][:, 0] - imin[0]] = vals[i]
        hvobj = hv.Image(np.flipud(im),
                         bounds=(imin[0], imin[1], imax[0], imax[1])).opts(
                             plot=dict(yaxis=None, xaxis=None))

    if coord is not None:
        hvobj *= hv.Curve([obj.hand2pixel((0,0)),obj.hand2pixel((coord,0))]) *\
            hv.Curve([obj.hand2pixel((0,0)),obj.hand2pixel((0,coord))])
    if tags:
        hvobj *= hv.Labels({
            'x': [obj._centers[i][0] for i in idx],
            'y': [obj._centers[i][1] for i in idx],
            'Label': [str(i) + ' ' + ''.join(obj.tags[i]) for i in idx]
        })

    # show cursor position in hand coordinates (works only in bokeh)
    if locator:
        pointer = hv.streams.PointerXY(x=0, y=0)
        dm = hv.DynamicMap(lambda x, y: hvobj * hv.Text(
            x, y + 5, '(%d,%d)' % tuple(obj.pixel2hand(np.array([x, y])))),
                           streams=[pointer])
        return dm

    return hvobj
Пример #12
0
 def _update_insert_plot(self):
     plot_data = DATA_A.loc[self.tool]
     data = [(plot_data["Xo"], plot_data["Yo"], plot_data[self.variable])]
     self.insert_plot_pane.object = hv.Path(data, vdims=self.variable).opts(
         cmap=self.color_map,
         color=self.variable,
         line_width=4,
         colorbar=True,
         responsive=True)
Пример #13
0
 def _boundary(self, data,  index):
     "Callback drawing Path element defining boundary"
     self._selected_edge_index = index
     xs, ys = data['x'], data['y']
     lines = []
     for i in range(len(xs)-1):
         s, e = i, (i+1)
         lines.append([(xs[s], ys[s]), (xs[e], ys[e])])
     self.ready = self._ready()
     return hv.Path(lines).opts(**self.edge_style)
Пример #14
0
def stem(data, curve=True, marker=True):
    '''stem plot:   data=(x,y,e)'''
    x, y, e = data
    vlines = [np.array([[x[i], y[i]], [x[i], e[i]]]) for i in range(len(x))]

    hs = hv.Path(vlines).opts(show_legend=True, muted_alpha=0.)

    if marker: hs = hs * hv.Scatter((x, e)).opts(size=4)
    if curve: hs = hs * hv.Curve((x, y)).opts(line_width=0.8)
    return hs
Пример #15
0
def plot_AGonia_boxes_interactive(data_path, Score, x, y):
    '''Function used as input for the dynamic map in function boxes_exploration_interactive():
    Same as plot_AGonia_boxes but now instead of having as input the index of the box the user
    can click on a box and it will use the (x,y) coordinates of the click to find the related box.
    The output now is only the boxes (all in red, selected in green)'''
    data_name, median_projection, fnames, fname_new, results_caiman_path, boxes_path = get_files_names(
        data_path)
    Yr, dims, T = cm.load_memmap(fname_new)
    images = np.reshape(Yr.T, [T] + list(dims), order='F')
    with open(boxes_path, 'rb') as f:
        boxes = pickle.load(f)
        f.close()
    boxes = boxes[boxes[:, 4] > Score].astype('int')
    roi_bounds = hv.Path([
        hv.Bounds(
            tuple([
                roi[0], median_projection.shape[0] - roi[3], roi[2],
                median_projection.shape[0] - roi[1]
            ])) for roi in boxes[:, :4]
    ]).options(color='red')

    if None not in [x, y]:
        try:
            box_idx = [
                i for i, box in enumerate(boxes) if x < box[2] and x > box[0]
                and y < (median_projection.shape[0] -
                         box[1]) and y > (median_projection.shape[0] - box[3])
            ][0]
        except:
            pass
    else:
        box_idx = 0
    #box_trace = images[:,boxes[box_idx,1]:boxes[box_idx,3],boxes[box_idx,0]:boxes[box_idx,2]].mean(axis=(1,2))
    box_square = hv.Path([
        hv.Bounds(
            tuple([
                boxes[box_idx,
                      0], median_projection.shape[0] - boxes[box_idx, 3],
                boxes[box_idx,
                      2], median_projection.shape[0] - boxes[box_idx, 1]
            ]))
    ]).options(color='lime')
    return (roi_bounds * box_square).opts(width=600, height=600)
Пример #16
0
def plot_sample(sample: np.ndarray):
    """
    Plots the gene counts for the given sample array as a bar-plot like single-path element.

    :param np.ndarray sample: An array containing the count values.

    :return:
    """
    paths = _create_paths(sample)
    plot = hv.Path(paths)
    return plot.opts(width=400, height=400, padding=0.05)
Пример #17
0
def plot_count_sum_mass(counts: np.ndarray, lengths: np.ndarray):
    """
    Plots the sum of the gene masses (along the sample axis) for the given count array.

    :param counts:
    :param lengths:
    :return:
    """
    sample_counts_sum = counts.sum(axis=0)
    mass_sums = sample_counts_sum * lengths
    paths = _create_paths(mass_sums)
    plot = hv.Path(paths)
    return plot.opts(width=400, height=400, padding=0.05)
Пример #18
0
def plot_sample_mass(sample: np.ndarray, lengths: np.ndarray):
    """
    Plots gene masses (counts * lengths) for the given sample array.

    :param np.ndarray sample: An array containing the count values.

    :param np.ndarray lengths: An array containing the length values.
    :return:
    """

    masses = sample * lengths
    paths = _create_paths(masses)
    plot = hv.Path(paths)
    return plot.opts(width=400, height=400, padding=0.05)
Пример #19
0
    def plot_path(self, data):
        # at this stage, plot is rendered and size known
        if not self.zoom_initialized:
            self.monitor_zoom_level()
            self.zoom_initialized = True

        path = hv.Path(data)
        path.opts(
            opts.Path(line_width=self.tool_width * self.zoom_level + 1,
                      color=self.cmap[self.dataset.drawing_label],
                      line_cap='round',
                      line_join='round'))

        return path
Пример #20
0
def mle_path_plot(bs_reps, param_1 = 'α*', param_2 = 'b*', conf = 0.95):
    # plot bootstrap samples, from BE/Bi103a lesson exercises
    points = hv.Points(data=bs_reps, kdims=[param_1, param_2]).opts(
        size=1, alpha=0.5, padding=0.1
    )

    # Get contour line
    xs, ys = bebi103.viz.contour_lines_from_samples(
        bs_reps[:, 0], bs_reps[:, 1], levels=conf
    )

    # Overlay with sample
    out = points * hv.Path(data=(xs[0], ys[0])).opts(color="black")
    
    return out
Пример #21
0
def track_plt(dataset,title, bounds=(-166,13.5,-154, 25)):
    """
    plot Jason-2 tracks from dataset with coordinates: N = [-90, 90], E = [-180, 180]
    """
    lon_range = (np.array(dataset['lon range']) % 180) - (np.array(dataset['lon range']) >= 180) * 180
    lat_range = np.array(dataset['lat range'])
    coast = gf.coastline.geoms('50m', bounds=bounds).opts(color='black',
                                                                        xticks=5,
                                                                        yticks=5,
                                                                        yformatter='%f °N',
                                                                        xformatter = '%f °E') 
    track = hv.Path((lon_range,lat_range)).opts(color = 'black')
    return (coast * track ).opts(projection=ccrs.PlateCarree(),
                                 title = title,
                                 height=200,
                                 width=200)
Пример #22
0
def spikes(data, y_base=0, dims=["Time", "x"], label="Signal", curve=True):
    '''plot stems and a curve'''
    if isinstance(data, tuple):
        t, s = data
    else:
        t = np.arange(0, len(data), 1)
        s = data

    vlines = [np.array([[t[i], y_base], [t[i], s[i]]]) for i in range(len(s))]

    hs = hv.Path(vlines, kdims=dims, label=label).opts(show_legend=True,
                                                       muted_alpha=0.,
                                                       color='black')

    if curve:
        hs = hs * hv.Curve(
            (t, s), dims[0], dims[1], label=label).opts(line_width=0.8)
    return hs
Пример #23
0
 def _make_progress(self):
     import holoviews as hv
     import holoviews.plotting.bokeh  # noqa
     stages = len(self._stages)
     line = hv.Path([[(0, 0), (stages - 1, 0)]]).options(line_width=10,
                                                         color='black',
                                                         backend='bokeh')
     vals = np.arange(stages)
     active = [1 if v == self._stage else 0 for v in vals]
     points = hv.Points((vals, np.zeros(stages), active),
                        vdims=['active']).options(color_index='active',
                                                  line_color='black',
                                                  cmap={
                                                      0: 'white',
                                                      1: 'gray'
                                                  },
                                                  show_legend=False,
                                                  size=20,
                                                  default_tools=[],
                                                  tools=['tap'],
                                                  nonselection_alpha=1,
                                                  backend='bokeh')
     point_labels = points.add_dimension('text',
                                         0, [n for n, _ in self._stages],
                                         vdim=True)
     labels = hv.Labels(point_labels).options(yoffset=-2.5, backend='bokeh')
     self._progress_sel.source = points
     hv_plot = (line * points * labels).options(xaxis=None,
                                                yaxis=None,
                                                width=800,
                                                show_frame=False,
                                                toolbar=None,
                                                height=80,
                                                xlim=(-0.5, stages - 0.5),
                                                ylim=(-4, 1.5),
                                                clone=False,
                                                backend='bokeh')
     return HoloViews(hv_plot, backend='bokeh')
Пример #24
0
    def _get_windows(self, xtrack=None, atrack=None, x=None, y=None):

        if x is not None:
            xtrack = x
        if y is not None:
            atrack = y

        # atrack and xtrack are from mouse or user. get the nearest where histogram is defined
        xtrack, atrack = self._get_xatrack(xtrack=xtrack, atrack=atrack)

        windows_list = []
        try:
            ws_list = self.gradients_hist['window_size']
        except KeyError:
            # no 'window_size'. compute it from axtrack neighbors
            ws_list = [
                np.diff(
                    np.array([[
                        self.gradients_hist[ax].isel({
                            ax: i
                        }).item() for i in [0, 1]
                    ] for ax in ['atrack', 'xtrack']])).mean()
            ]

        for ws in ws_list:
            # window as a hv.Path object corresponding to window_size
            amin, amax, xmin, xmax = (atrack - ws / 2, atrack + ws / 2,
                                      xtrack - ws / 2, xtrack + ws / 2)
            try:
                style = self._get_style(
                    self.gradients_hist.sel(window_size=ws))
            except (IndexError, KeyError):
                style = {}
            windows_list.append(
                hv.Path([[(xmin, amin), (xmin, amax), (xmax, amax),
                          (xmax, amin), (xmin, amin)]]).opts(**style))

        return hv.Overlay(windows_list)
Пример #25
0
from random import randint

import holoviews as hv
import panel as pn
from holoviews import opts, streams

LINE = "red"
FILL1 = "green"
FILL2 = "fulvous"
FILL3 = "blue"

hv.extension("bokeh")
path = hv.Path([[(1, 5), (9, 5)]])
poly = hv.Polygons([[(2, 2), (5, 8), (8, 2)]])
path_stream = streams.PolyDraw(source=path, drag=True, show_vertices=True)
poly_stream = streams.PolyDraw(
    source=poly,
    drag=True,
    num_objects=4,
    show_vertices=True,
    styles={"fill_color": [FILL1, FILL2, FILL3]},
)


def get_plot(path, poly):
    return (path * poly).opts(
        opts.Polygons(fill_alpha=0.3, active_tools=["poly_draw"]),
        opts.Path(color=LINE, height=400, line_width=5, width=400),
    )

Пример #26
0
    def analysis_gate(self, df):
        # global curr_savgol_plot, maxsavgol
        gate_files.append(df)  # append to a list containing all the gate diode files

        # Remove initial kink from the data
        start_value = np.mean(self.data[df]["data"]["Current"][10:20])
        CurrentCopy = self.data[df]["data"]["Current"].copy()
        for x in range(int(len(self.data[df]["data"]["Current"]) / 2)):
            if CurrentCopy[x] < start_value:
                CurrentCopy[x] = start_value

        # Generate curve
        plot_not_kink = self.add_single_plots(
            self.data[df]["data"][self.xaxis], CurrentCopy, "Current"
        )

        # Try savgol filter
        ##try:
        ##    i = 0
        ##    while i < 2:
        ##        curr_savgol = scipy.signal.savgol_filter(self.data[df]['data']['Current'], 31, 3)  # window size 51, polynomial order 3
        ##        i += 1
        ##    maxsavgol = max(curr_savgol)
        ##    curr_savgol_plot = self.add_single_plots(self.data[df]['data']['Voltage'], curr_savgol, "SavgolCurrent")
        ##except Exception:
        ##    self.log.warning("No savgol plot possible... Error: {}")

        # Interpolation current curve
        xnew, ynew = self.interpolated_axis(
            df, self.data[df]["data"][self.xaxis], CurrentCopy
        )
        curr_interp_plot = self.add_single_plots(xnew, ynew, "InterpolatedCurrent")

        # Build the first derivatives
        firstderi_interp = self.build_first_derivative(xnew, ynew)
        dif_intep_plot = self.add_single_plots(
            xnew, firstderi_interp, "FirstDerivativeCurrent"
        )

        # Second derivative
        second_deriv_interp = self.build_second_derivative(xnew, ynew)
        dif2_intep_plot = self.add_single_plots(
            xnew, second_deriv_interp, "SecondDerivativeCurrent"
        )

        # Not interpolated first derivative
        firstdev_not_interp = self.build_first_derivative(
            self.data[df]["data"]["Current"], self.data[df]["data"]["Voltage"]
        )
        self.insert_in_df(df, 3, "firstderivative_gate", firstdev_not_interp)

        # Try to find the start and ending indices of the points where you want to average, used to handle the problematic files
        max1_index = list(firstderi_interp).index(max(firstderi_interp))
        min1_index = list(firstderi_interp).index(min(firstderi_interp))
        if min1_index < max1_index:
            min1_index = max1_index + 1
            max1_index = min1_index - 2
        median_index = int(len(xnew) / 2)
        if median_index < max1_index:
            median_index = max1_index + 1
        if min1_index < median_index:
            min1_index = median_index + 1
            max1_index = median_index - 1

        # Find the segment where you want to average using the second derivative
        interesting_section = sorted(
            list(second_deriv_interp[max1_index:median_index]), reverse=True
        )
        firstminimum = interesting_section[0]
        interesting_section2 = sorted(
            list(second_deriv_interp[median_index:min1_index]), reverse=True
        )
        second_minimum = interesting_section2[0]

        # Find average
        start_average = list(second_deriv_interp).index(firstminimum)
        end_average = list(second_deriv_interp).index(second_minimum)
        I_surf_maxima_average = np.mean(list(ynew[start_average:end_average]))

        # Compute the surface current with the average method
        mxx = max(ynew)  # find maximum value of the current-voltage curve
        miny = np.mean(
            list(ynew[-1000:])
        )  # find the minimum of the current-voltage curve by averaging 20 points values in the curve tail
        I_surf_average = (
            I_surf_maxima_average - miny
        )  # compute the surface current by computing the difference between the maximum and minimum value
        I_surf_average_table = "{:.2e}".format(I_surf_average)
        Surface_current_average.append(I_surf_average_table)

        # Compute surface current with the maximum method
        Isurf_max = (
            mxx - miny
        )  # compute the surface current by computing the difference between the maximum and minimum value
        Isurf_table = "{:.2e}".format(Isurf_max)
        Surface_current.append(Isurf_table)

        # Compute Surface_recombination_velocity with the maximum method
        S_null_max = Isurf_max / (
            self.config["IV_PQC_parameter"]["q"]
            * self.config["IV_PQC_parameter"]["n_i_intrinsic_carrier_concentration"]
            * (float(self.data[df]["header"][0].split(":")[1]) * (1e-8))
        )
        Surface_recombination_velocity.append(S_null_max)

        # Compute Surface_recombination_velocity with the average method
        S_null_average = I_surf_average / (
            self.config["IV_PQC_parameter"]["q"]
            * self.config["IV_PQC_parameter"]["n_i_intrinsic_carrier_concentration"]
            * (float(self.data[df]["header"][0].split(":")[1]) * (1e-8))
        )
        Surface_recombination_velocity_average.append(S_null_average)

        # Add text to the plot
        text = hv.Text(
            3,
            9 * (1e-11),
            "Isurf_max: {} A\n"
            "Isurf_average: {} A\n"
            "Surface recombination velocity_max: {} cm/s\n"
            "Surface recombination velocity_average: {} cm/s".format(
                np.round(Isurf_max, 15),
                np.round(I_surf_average, 15),
                np.round(S_null_max, 4),
                np.round(S_null_average, 4),
            ),
        ).opts(style=dict(text_font_size="20pt"))

        # Do this if the analysis is of just one file.
        if len(gate_files) == 1:
            # Add overlaid lines on the plot
            Path_min = hv.Path([(-2, miny), (6, miny)]).opts(line_width=2.0)
            Path_mxx = hv.Path([(-2, mxx), (6, mxx)]).opts(line_width=2.0)
            ##Path_savgolmax = hv.Path([(-2, maxsavgol), (6, maxsavgol)]).opts(line_width=2.0)
            Path_average = hv.Path(
                [(-2, I_surf_maxima_average), (6, I_surf_maxima_average)]
            ).opts(line_width=2.0)
            Path_Isurf = hv.Arrow(-1, mxx, "max", "^")
            Path_Isurf_average = hv.Arrow(0, I_surf_maxima_average, "average", "^")
            # Plot all Measurements
            self.donts_gatediode = [
                "timestamp",
                "voltage",
                "Voltage",
                "Current",
                "Stepsize",
                "Wait",
                "Stepsize",
                "Frequency",
                "x",
                "N",
            ]  # don't plot these.
            self.PlotDict["BasePlots_gate"] = plot_not_kink
            self.PlotDict["BasePlots_gate"] += dif_intep_plot
            self.PlotDict["BasePlots_gate"] += dif2_intep_plot
            self.PlotDict["BasePlots_gate"] += curr_interp_plot
            try:
                self.PlotDict["BasePlots_gate"] += curr_savgol_plot
                self.PlotDict["BasePlots_gate"] += curr_savgol_plot * plot_not_kink
            except Exception:
                self.log.warning("No savgol plot possible... Error: {}")
            self.PlotDict["BasePlots_gate"] += (
                text
                * plot_not_kink
                * Path_min
                * Path_mxx
                * Path_average
                * Path_Isurf
                * Path_Isurf_average
            )  # * Path_savgolmax
            self.PlotDict["BasePlots_gate"] += (
                curr_interp_plot * dif_intep_plot * dif2_intep_plot * plot_not_kink
            )

            # Add table that shows resulting parameters of the analysis
            df4 = pd.DataFrame(
                {
                    "Name": gate_files,
                    "Surface current_max (A)": Surface_current,
                    "Surface current_average (A)": Surface_current_average,
                    "Surface recombination velocity_max (cm/s)": Surface_recombination_velocity,
                    "Surface recombination velocity_average (cm/s)": Surface_recombination_velocity_average,
                }
            )
            table3 = hv.Table((df4), label="Gate analysis")
            table3.opts(width=1300, height=800)
            self.PlotDict["BasePlots_gate"] += table3

        # Do this if the analysis is of more than one file
        elif len(gate_files) > 1:
            self.donts = [
                "timestamp",
                "voltage",
                "Voltage",
                "Stepsize",
                "Wait",
                "Stepsize",
                "Frequency",
                "firstderivative_gate",
                "x",
                "N",
            ]  # do not plot this data
            self.basePlots3 = plot_all_measurements(
                self.data,
                self.config,
                self.xaxis,
                self.name,
                do_not_plot=self.donts,
                keys=gate_files,
            )
            self.PlotDict["BasePlots_gate"] = self.basePlots3

            # Add table that shows resulting parameters of the analysis
            df4 = pd.DataFrame(
                {
                    "Name": gate_files,
                    "Surface current_max (A)": Surface_current,
                    "Surface current_average (A)": Surface_current_average,
                    "Surface recombination velocity_max (cm/s)": Surface_recombination_velocity,
                    "Surface recombination velocity_average (cm/s)": Surface_recombination_velocity_average,
                }
            )
            table3 = hv.Table((df4), label="Gate analysis")
            table3.opts(width=1300, height=800)
            self.PlotDict["BasePlots_gate"] += table3

        return self.PlotDict["BasePlots_gate"]
Пример #27
0
def create_plot(
    data,
    x,
    y,
    plot_type="scatter",
    selected=None,
    show_selected=True,
    slow_render=False,
    legend=True,
    colours=True,
    smaller_axes_limits=False,
    bounds=None,
    legend_position=None,
):
    assert x in list(
        data.columns), f"Column {x} is not a column in your dataframe."
    assert y in list(
        data.columns), f"Column {y} is not a column in your dataframe."

    if bounds is not None:
        data = data[data[x] >= bounds[0]]
        data = data[data[y] <= bounds[1]]
        data = data[data[x] <= bounds[2]]
        data = data[data[y] >= bounds[3]]

    if plot_type == "scatter":
        p = hv.Points(
            data,
            [x, y],
        ).opts(active_tools=["pan", "wheel_zoom"])
    elif plot_type == "line":
        p = hv.Path(
            data,
            [x, y],
        ).opts(active_tools=["pan", "wheel_zoom"])
    if show_selected:

        if selected is not None:
            cols = list(data.columns)

            if len(selected.data[cols[0]]) == 1:
                selected = pd.DataFrame(selected.data, columns=cols, index=[0])
                if bounds is not None:
                    if ((selected[x][0] < bounds[0])
                            or (selected[y][0] > bounds[1])
                            or (selected[x][0] > bounds[2])
                            or (selected[y][0] < bounds[3])):
                        selected = pd.DataFrame(columns=cols)
            else:
                selected = pd.DataFrame(columns=cols)

            selected_plot = hv.Scatter(
                selected,
                x,
                y,
            ).opts(
                fill_color="black",
                marker="circle",
                size=10,
                active_tools=["pan", "wheel_zoom"],
            )

    if colours:
        color_key = config.settings["label_colours"]

        color_points = hv.NdOverlay({
            config.settings["labels_to_strings"][f"{n}"]:
            hv.Points([0, 0],
                      label=config.settings["labels_to_strings"][f"{n}"]).opts(
                          style=dict(color=color_key[n], size=0))
            for n in color_key
        })

    if smaller_axes_limits:

        max_x = np.max(data[x])
        min_x = np.min(data[x])

        max_y = np.max(data[y])
        min_y = np.min(data[y])

        x_sd = np.std(data[x])
        x_mu = np.mean(data[x])
        y_sd = np.std(data[y])
        y_mu = np.mean(data[y])

        max_x = np.min([x_mu + 4 * x_sd, max_x])
        min_x = np.max([x_mu - 4 * x_sd, min_x])

        max_y = np.min([y_mu + 4 * y_sd, max_y])
        min_y = np.max([y_mu - 4 * y_sd, min_y])

        if show_selected:
            if selected is not None:
                if selected.shape[0] > 0:

                    max_x = np.max([max_x, np.max(selected[x])])
                    min_x = np.min([min_x, np.min(selected[x])])

                    max_y = np.max([max_y, np.max(selected[y])])
                    min_y = np.min([min_y, np.min(selected[y])])

    if colours:
        if smaller_axes_limits:
            plot = dynspread(
                datashade(
                    p,
                    color_key=color_key,
                    aggregator=ds.by(config.settings["label_col"], ds.count()),
                ).opts(xlim=(min_x, max_x),
                       ylim=(min_y, max_y),
                       responsive=True),
                threshold=0.75,
                how="saturate",
            )
        else:
            plot = dynspread(
                datashade(
                    p,
                    color_key=color_key,
                    aggregator=ds.by(config.settings["label_col"], ds.count()),
                ).opts(responsive=True),
                threshold=0.75,
                how="saturate",
            )

    else:
        if smaller_axes_limits:
            plot = dynspread(
                datashade(p, ).opts(xlim=(min_x, max_x),
                                    ylim=(min_y, max_y),
                                    responsive=True),
                threshold=0.75,
                how="saturate",
            ).redim.range(xdim=(min_x, max_x), ydim=(min_y, max_y))
        else:
            plot = dynspread(
                datashade(p, ).opts(responsive=True),
                threshold=0.75,
                how="saturate",
            )

    if slow_render:
        plot = p

    if show_selected and (selected is not None):
        plot = plot * selected_plot

    if legend and colours:
        plot = plot * color_points

    if legend_position is not None:
        plot = plot.opts(legend_position=legend_position)

    return plot
Пример #28
0
def make_plot(t):
    spring = hv.Scatter(([q(t)], [0])).opts(xlabel='q', size=6, show_grid=True, title="Spring")
    phase_space = hv.Scatter(([q(t)], [p(t)])).opts(xlabel="q", ylabel='p', size=6, show_grid=True, title="Phase Space")
    trajectory = hv.Path(([q(_t) for _t in np.linspace(0, 30, 200)], [p(_t) for _t in np.linspace(0, 30, 200)])).opts(xlabel="q", ylabel='p(x)', line_width=2, color="orange")
    return (spring+ (phase_space * trajectory)).opts(shared_axes=False)
Пример #29
0
def make_rect(bounds=DEFAULT_BOUNDS, color='blue'):
    minX, minY, maxX, maxY = bounds
    return hv.Path([(minX, minY), (maxX, minY), (maxX, maxY), (minX, maxY),
                    (minX, minY)]).opts(img_opts).opts(line_width=2,
                                                       color='blue')
import numpy as np
import pandas as pd
import bokeh
import holoviews as hv
import datashader as ds
from holoviews.operation.datashader import aggregate, shade, datashade, dynspread
from holoviews.operation import decimate

SOURCE = '../data_preprocessed/TEs_CHR1.hdf'
HEIGHT = 350
WIDTH = 800

reads = pd.read_hdf(SOURCE, key='tes')

plot = dynspread(
    datashade(hv.Path([reads]),
              min_alpha=100,
              aggregator=ds.count_cat('family'))).options(width=WIDTH,
                                                          height=HEIGHT,
                                                          bgcolor="black")

#renderer = hv.renderer('bokeh')
#renderer.save(plot, '/Users/cfltxm/Docs/talks/QRWbioinformatics2018/talk/fig/plot_4_te_reads')

doc = hv.renderer('bokeh').server_doc(plot)