Example #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)
Example #2
0
 def plotMap(self, show=True):
     ''' Plot the map'''
     #print(self.mapUrl)
     option = '?list_dir=no'
     vsiCurl = f'/vsicurl/{option}&url={self.mapUrl}'
     #
     da = rioxarray.open_rasterio(vsiCurl,
                                  overview_level=3,
                                  parse_coordinates=True,
                                  chunks=dict(band=1, y=512, x=512),
                                  masked=False).squeeze('band')
     img = da.hvplot.image(rasterize=True,
                           cmap='gray',
                           x='x',
                           y='y',
                           aspect='equal',
                           frame_width=400,
                           title=os.path.basename(self.mapUrl)).opts(
                               active_tools=['box_select'])
     self.box.source = img
     bounds = hv.DynamicMap(lambda bounds: hv.Bounds(bounds),
                            streams=[self.box]).opts(color='red')
     if show:
         mapview = pn.Column(img * bounds)
     else:
         mapview = None
     return mapview
Example #3
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)
Example #4
0
    def box_chooser(self, **kwargs):
        '''
        Visual tool for choosing rectangle shaped blocks in mask.
        :param kwargs: array, if you want to pre select pixels,
                       initialize_mask, True if you want to initialize mask.
                                        True also Overrides array.
                                        False does nothing and overrides
                                        nothing
                       initialize_value, what value you want to use in
                                        initialisation. Used if initialize_mask
                                        is True. Fallback value is 0.
        :return: hv.Object
        '''
        # cube.test_for_not_none()
        wid = len(self._obj.coords[self._obj.M.dims[1]])
        hei = len(self._obj.coords[self._obj.M.dims[0]])
        # dep = len(cube.band)

        self.__do_mask_changes(**kwargs)
        all_pixels = np.array(list(itertools.product(range(wid), range(hei))))
        mask_dims = self._obj.M.dims.copy()
        mask_dims.reverse()
        points = hv.Points(all_pixels, kdims=mask_dims)
        # print(points)
        ds_attrs = self._make_dataset_opts()
        dataset3d = hv.Dataset(**ds_attrs)
        box = hv.streams.BoundsXY(source=points, bounds=(0, 0, 0, 0))
        bounds = hv.DynamicMap(lambda bounds: hv.Bounds(bounds), streams=[box])
        third_dim_list = list(self._obj.dims)
        third_dim_list.remove(mask_dims[0])
        third_dim_list.remove(mask_dims[1])
        layout = decimate(points) * \
                 dataset3d.to(hv.Image,
                              mask_dims,
                              'Value',
                              third_dim_list) * \
                 bounds + \
                 decimate(
                     hv.DynamicMap(
                         lambda bounds: hv.Points(
                             self._record_selections(bounds, points),
                             kdims=mask_dims
                         ),
                         streams=[box]
                     )
                 )
        return layout
Example #5
0
    def spectre_chooser(self, **kwargs):
        '''
        Visual tool
        for visualizing selected spectra and narrowing down mask based
        on spectra. It is easy to make a mistake with this, so you should
        keep a backup of your mask. You shouldn't use this on very large sets.
        TODO: This algorithm seems quite unefficient, maybe adding threading
        TODO:        could help.
        :param kwargs: array, if you want to pre select pixels,
                       initialize_mask, True if you want to initialize mask.
                                        True also Overrides array.
                                        False does nothing and overrides
                                        nothing
                       initialize_value, what value you want to use in
                                        initialisation. Used if initialize_mask
                                        is True. Fallback value is 0.
        :return: hv.Object
        '''
        if not len(self._obj.shape) == 3:
            raise ValueError('The spectre_chooser only works for 3 ' +
                             'dimensional objects.')
        self.__do_mask_changes(**kwargs)
        third_dimension = self._obj.M.no_mask_dims[0]
        third_dim_list = self._obj.coords[third_dimension].data
        points = hv.Points(
            np.array([(np.min(third_dim_list), np.min(self._obj.data))]))
        box = hv.streams.BoundsXY(
            source=points,
            # bounds is defined as (x_min, y_min, x_max, y_max)
            bounds=(np.min(third_dim_list) - 0.001, np.min(self._obj.data) -
                    0.001, np.max(third_dim_list) + 0.001,
                    np.max(self._obj.data) + 0.01))

        bounds = hv.DynamicMap(lambda bounds: hv.Bounds(bounds), streams=[box])
        layout = points *\
                 hv.DynamicMap(
                     lambda bounds: hv.Overlay(
                         [hv.Curve((third_dim_list,zs),
                                   kdims=self._obj.M.no_mask_dims,
                                   vdims=['Value'])
                          for zs in self._new_choosing_spectre(bounds)]
                     ),
                     streams=[box]) * \
                 bounds
        return layout
Example #6
0
def test_bounds(bounds):
    global selection, vizual, gvplot, hvplot, heatmap, feats, points, world_map, range_slider, controls3, max_cur_feature, min_cur_feature, temp_feats
    if bounds != None:
        min_long = min(bounds[0], bounds[2])
        max_long = max(bounds[0], bounds[2])
        min_lat = min(bounds[1], bounds[3])
        max_lat = max(bounds[1], bounds[3])
        downscale_feats = loc_feats.loc[(loc_feats['Longitude'] >= min_long)
                                        & (loc_feats['Longitude'] <= max_long)
                                        & (loc_feats['Latitude'] >= min_lat) &
                                        (loc_feats['Latitude'] <= max_lat)]
        temp_feats = downscale_feats
        max_cur_feature = temp_feats[choice.value].max()
        min_cur_feature = temp_feats[choice.value].min()
        range_slider = RangeSlider(start=min_cur_feature,
                                   end=max_cur_feature,
                                   value=(min_cur_feature, max_cur_feature),
                                   step=(max_cur_feature - min_cur_feature) /
                                   20,
                                   title="Feature_range")
        range_slider.on_change('value', update_map_val)
        controls3 = widgetbox([range_slider], width=250)
        new_loc_feats = temp_feats.loc[
            (temp_feats[choice.value] <= range_slider.value[1])
            & (temp_feats[choice.value] >= range_slider.value[0])]
        feats = gv.Dataset(new_loc_feats,
                           kdims=['Longitude', 'Latitude', choice.value])
        points = feats.to(gv.Points, ['Longitude', 'Latitude'], [choice.value])
        if len(new_loc_feats <= 20000):
            world_map = gv.Points(points).options(
                'Points',
                size=5,
                cmap='viridis',
                colorbar=True,
                tools=TOOLS,
                color_index=2,
                width=900,
                height=800,
                colorbar_opts={'scale_alpha': 0.5},
                fill_alpha=0.5,
                line_alpha=0.5)
        else:
            world_map = decimate(gv.Points(points), max_samples=20000).options(
                'Points',
                size=5,
                cmap='viridis',
                colorbar=True,
                tools=TOOLS,
                color_index=2,
                width=900,
                height=800,
                colorbar_opts={'scale_alpha': 0.5},
                fill_alpha=0.5,
                line_alpha=0.5)
        selection = hv.streams.Selection1D(source=world_map)
        heatmap = hv.DynamicMap(selected_points, streams=[selection])
        box = hv.streams.BoundsXY(source=world_map)
        zoom = hv.DynamicMap(test_bounds, streams=[box])
        hvplot = renderer.get_plot(heatmap, curdoc())
        gvplot = renderer.get_plot(world_map, curdoc())
        bvplot = renderer.get_plot(zoom, curdoc())
        vizual.children[1].children = [
            gvplot.state, hvplot.state, bvplot.state
        ]
        vizual.children[0].children[2] = controls3
    else:
        bounds = (0, 0, 0, 0)
    return hv.Bounds(bounds).options(show_grid=False,
                                     height=0,
                                     width=0,
                                     xaxis=None,
                                     yaxis=None,
                                     default_tools=[],
                                     show_frame=False,
                                     toolbar=None)
from holoviews import streams, opts
from holoviews.operation.datashader import datashade
from holoviews.plotting.plotly.dash import to_dash

points = hv.Points(
    np.random.multivariate_normal((0, 0), [[1, 0.1], [0.1, 1]], (1000, )))

sel = streams.Selection1D(source=points)
mean_sel = hv.DynamicMap(lambda index: hv.HLine(points['y'][index].mean()
                                                if index else -10),
                         kdims=[],
                         streams=[sel])

# Declare a Bounds stream and DynamicMap to get box_select geometry and draw it
box = streams.BoundsXY(source=points, bounds=(0, 0, 0, 0))
bounds = hv.DynamicMap(lambda bounds: hv.Bounds(bounds), streams=[box])

# Declare DynamicMap to apply bounds selection
dmap = hv.DynamicMap(lambda bounds: points.select(x=(bounds[0], bounds[2]),
                                                  y=(bounds[1], bounds[3])),
                     streams=[box])

# Compute histograms of selection along x-axis and y-axis
yhist = hv.operation.histogram(dmap,
                               bin_range=points.range('y'),
                               dimension='y',
                               dynamic=True,
                               normed=False)
xhist = hv.operation.histogram(dmap,
                               bin_range=points.range('x'),
                               dimension='x',
Example #8
0
###########################
### using the csv files ###
###########################

anotations_all = pd.read_csv('/media/pedro/DATAPART1/AGOnIA/datasets_figure/notazione finale/NEU LARGE/test.csv')

anotations_all.columns=['experiment','xmin','ymin','xmax','ymax','id']
anotations_all.loc[anotations_all['experiment']=='neurofinder.01.01.bmp']
boxes_csv = []
for index, row in anotations_all.loc[anotations_all['experiment']=='neurofinder.01.01.bmp'].iterrows():
    boxes_csv.append([row['xmin'],row['ymin'],row['xmax'],row['ymax']])
np.shape(boxes_csv)
np.shape(boxes)

##################################
### load agonia boxes and plot ###
##################################


data_path = '/media/pedro/DATAPART1/AGOnIA/datasets_figure/neurofinder/neurofinder.01.01'
data_name,median_projection,fnames,fname_new,results_caiman_path,boxes_path = ut.get_files_names(data_path)

boxes_agonia = pickle.load(open(boxes_path,'rb'))
boxes_agonia=boxes_agonia[boxes_agonia[:,4]>.15].astype(int)[:,:4]
boxes_agonia.shape
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_agonia[:,:4]]).options(color='red')
roi_bounds_true = 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_csv]).options(color='green')
img = hv.Image(median_projection,bounds=(0,0,median_projection.shape[1],median_projection.shape[0])).options(cmap='gray')
(img*roi_bounds*roi_bounds_true).opts(fig_size=300)