コード例 #1
0
    def __init__(self, path, ping_file_path, speed_test_file_path):

        self.path = path
        self.ping_file_path = ping_file_path
        self.speed_test_file_name = speed_test_file_path

        # Define default layout of graphs
        hv.extension('bokeh')

        opts.defaults(
            opts.Bars(xrotation=45, tools=['hover']),
            opts.BoxWhisker(width=700, xrotation=30, box_fill_color=Palette('Category20')),
            opts.Curve(width=700, tools=['hover']),
            opts.GridSpace(shared_yaxis=True),
            opts.Scatter(width=700, height=500, color=Palette('Category20'), size=dim('growth')+5, tools=['hover'],alpha=0.5, cmap='Set1'),
            opts.NdOverlay(legend_position='left'))

        if os.path.isdir(os.path.join(self.path, "webpage","figures")) is False:
            os.mkdir(os.path.join(self.path, "webpage","figures"))
            print("Path 'figures' created successfully")
        else:
            print("Path 'figures' initialized")
        # Load basic configurations
        config = configparser.ConfigParser()

        try:
            config.read('./modules/config_a.ini')
            # Get values from configuration file
            self.upper_acceptable_ping_bound = float(config['DEFAULT']['upper_acceptable_ping_bound'])
            self.upper_ping_issue_bound = float(config['DEFAULT']['upper_ping_issue_bound'])
            self.acceptable_network_speed = float(config['DEFAULT']['acceptable_network_speed'])
        except:
            # In case no config-file is found or another reading error occured
            print("Configuration file not found/readable.")
            print("Creating a new configuration file.")
            # Creating new file with standard values
            config['DEFAULT'] = {'upper_acceptable_ping_bound': '10',
                                 'upper_ping_issue_bound': '99999',
                                 'acceptable_network_speed': '16'}
            with open('config_a.ini', 'w') as configfile:
                config.write(configfile)
            print("New configuration file was created. Running on default parameters, please restart for changes.")

            #set default values to continue with program
            self.upper_acceptable_ping_bound = float(config['DEFAULT']['upper_acceptable_ping_bound'])
            self.upper_ping_issue_bound = float(config['DEFAULT']['upper_ping_issue_bound'])
            self.acceptable_network_speed = float(config['DEFAULT']['acceptable_network_speed'])
コード例 #2
0
class BasemapModule:
    """
    NAME
    ----
        BasemapModule

    DESCRIPTION
    -----------
        Blueprint for Basemap objects.
        
        Plots seismic survey elements such as polygon, wells, lines and the intersection of these 
        lines while providing interactive tools to improve the experience between data and users. 
        These plots are not images but objects that can be modified by the user and exported as 
        images.
    
    ATTRIBUTES
    ----------
        basemap_dataframe : (Pandas)DataFrame
            Matrix compounded by the coordinates and lines of the seismic survey's corners. 
            Empty by default.

        wells_dataframe : (Pandas)DataFrame
            Matrix compounded by wells related information. Empty by default.

        polygon : Holviews element [Curve]
            Plot of the seismic survey polygon.

        wells : Holviews element [Scatter]
            Plot of the wells inside the seismic survey.

        seismic_lines : Holviews element [Overlay]
            Plot of the seismic lines (Inline referred as iline and Crossline referred as xline) 
            and its intersection.
            
        basemap : Holviews element [Overlay]
            Combination of the plots: polygon, wells and seismic_lines.
        
    METHODS
    -------
        polygon_plot(**kwargs)
            Constructs the polygon attribute.

        wells_plot(**kwargs)
            Constructs the wells attribute.   

        seismic_line_plot(**kwargS)
            Constructs the seismic_lines attribute.

        get_basemap(**kwargs)
            Constructs the Basemap attribute and provides interactive methods to
            inspect the plotted data.
            
    LIBRARIES
    ---------
        Holoviews: BSD open source Python library designed to simplify the visualization of data.
                    More information available at:
                        http://holoviews.org/

	Numpy: BSD licensed package for scientific computing with Python. More information
               available at:
                   https://numpy.org/
    
        Pandas: BSD 3 licensed open source data analysis and manipulation tool, built on top of
                the Python programming language. More information available at:
                    https://pandas.pydata.org/
                     
        Panel: BSD open source Python library that allows to create custom interactive dashboards 
               by connecting user defined widgets to plots. More information available at:
                    https://panel.holoviz.org/index.html
       
    ON PROGRESS
    -----------
        Include a GIS element into plots.
    """

    # Holoviews default config
    plot_tools = ['pan', 'wheel_zoom', 'reset']
    font_s = {"title": 16, "labels": 14, "xticks": 10, "yticks": 10}
    opts.defaults(opts.Curve(tools=plot_tools,
                             default_tools=[],
                             xformatter='%.0f',
                             yformatter='%.0f',
                             fontsize=font_s,
                             height=400,
                             width=400,
                             padding=0.1,
                             toolbar='right'),
                  opts.Scatter(tools=plot_tools,
                               default_tools=[],
                               xformatter='%.0f',
                               yformatter='%.0f',
                               fontsize=font_s,
                               height=400,
                               width=400,
                               padding=0.1,
                               toolbar='right',
                               framewise=True,
                               show_grid=True),
                  opts.GridSpace(fontsize=font_s,
                                 shared_yaxis=True,
                                 plot_size=(120, 380),
                                 toolbar="left"),
                  opts.Overlay(xformatter='%.0f',
                               yformatter='%.0f',
                               fontsize=font_s,
                               toolbar="left",
                               show_grid=True),
                  opts.Points(tools=['box_select', 'lasso_select'],
                              default_tools=[],
                              active_tools=["box_select"],
                              size=3,
                              width=500,
                              height=400,
                              padding=0.01,
                              fontsize={
                                  'title': 16,
                                  'ylabel': 14,
                                  'xlabel': 14,
                                  'ticks': 10
                              },
                              framewise=True,
                              show_grid=True),
                  toolbar="left")

    def __init__(self, basemap_dataframe, wells_dataframe):
        """
        DESCRIPTION
        -----------
            Instantiates BasemapModule's attributes. For more information, please refer to 
            BasemapModule's docstring.
        
        """

        self.basemap_dataframe = basemap_dataframe
        self.wells_dataframe = wells_dataframe
        self.iline_step = 1
        self.xline_step = 1
        self.hover_format = [("Utmx", "$x{(0.00)}"), ("Utmy", "$y{(0.00)}")]
        self.hover_attributes = {
            "show_arrow": True,
            "point_policy": "follow_mouse",
            "anchor": "bottom_right",
            "attachment": "above",
            "line_policy": "none"
        }

    def polygon_plot(self):
        """
        NAME
        ----
            polygon_plot
        
        DESCRIPTION
        -----------
            Constructs the polygon attribute.
            
            Plots the boundaries of the seismic survey using Holoviews and bokeh as backend.
                   
        ARGUMENTS
        ---------
            BasemapModule.basemap_dataframe : (Pandas)DataFrame
                Matrix compounded by the coordinates and lines of the seismic survey's corners.
        
        RETURN
        ------
            BasemapModule.polygon : Holviews element [Curve] instance attribute
                Plot of the seismic survey polygon.
        
        """

        #Plotting the boundaries of the Seismic Survey. Holoviews Curve element
        BasemapModule.polygon = hv.Curve(self.basemap_dataframe,
                                         ["utmx", "utmy"],
                                         label="Polygon")
        BasemapModule.polygon.opts(line_width=2, color="black")

        return BasemapModule.polygon

    def wells_plot(self):
        """
        NAME
        ----
            wells_plot
        
        DESCRIPTION
        -----------
            Constructs the wells attribute

            Plots the wells inside the Seismic Survey's polygon using Holoviews and bokeh as
            backend.

        ARGUMENTS
        ---------
            BasemapModule.wells_dataframe : (Pandas)DataFrame
                Matrix compounded by wells related information.
            
            
        RETURN
        ------
            BasemapModule.wells : Holviews element [Scatter] instance attribute
                Plot of the wells inside the seismic survey.
            
        """

        # Declaring the Hover tools (each line will use one)
        wells_hover = HoverTool(tooltips=[("Well", "@name")] +
                                self.hover_format + [("Depth", "@depth{(0)}")])

        # Plotting Wells. Holoviews Scatter element
        BasemapModule.wells = hv.Scatter(
            self.wells_dataframe, ["utmx", "utmy"],
            ["name", "cdp_iline", "cdp_xline", "depth"],
            label="Wells")
        BasemapModule.wells.opts(line_width=1,
                                 color="green",
                                 size=10,
                                 marker="^")
        return (BasemapModule.wells)

    def seismic_line_plot(self, iline_number, xline_number):
        """
        NAME
        ----
            seismic_line_plot
            
        DESCRIPTION
        -----------
            Constructs the seismic_lines attribute.

            Plots seismic lines (given a set of inline and crossline numbers) and the intersection of
            these using Holoviews and bokeh as backend.
            
        ARGUMENTS
        ---------
            iline_number : int
                Number of the chosen inline. The value can be given manually or by Panel's slider 
                widget.

            xline_number : int
                Number of the chosen crossline. The value can be given manually or by Panel's slider 
                widget.

        RETURN
        ------
            BasemapModule.seismic_lines : Holviews element [Overlay] instance attribute
                Plot of the seismic lines and its intersection.
        
        FUNCTIONS
        ---------
            seismic_lines_dataframe(**kwargs)
                Builds a DataFrame for the first line either along inline or crossline direction.

            seismic_intersection(**kwargs)
                Computes the coordinates and tracf of the intersection between two seismic lines.
        
        REFERENCES
        ----------
            bokeh.pydata.org. Change the attributes of the hover tool. Online document:
        https://bokeh.pydata.org/en/latest/docs/reference/models/tools.html#bokeh.models.tools.HoverTool.point_policy
            
        """
        def seismic_lines_dataframe(line_direction, perpendicular_direction):
            """
            NAME
            ----
                seismic_lines_dataframe
                
            DESCRIPTION
            -----------
                Builds a DataFrame for the first line either along inline or crossline direction.

                The coordinates represent the lower end of a seismic line; therefore, these shall be used to
                draft seismic lines after the computation of the higher end. If the users want to plot a line 
                along inline direction, the code will compute the coordinates of the traces within the first 
                crossline and vice versa.

            ARGUMENTS
            ---------
            basemap_dataframe : (Pandas)DataFrame
                Matrix compounded by the coordinates and lines of the seismic survey's corners.

            line_direction : str
                Seismic line direction.

            perpendicular_direction : str
                Direction in which the points are going to be calculated. Is perpendicular to line_direction 
                argument.


            RETURN
            ------
                dlines : (Pandas)DataFrame
                    Contains the trace coordinates within the first seismic line.
                    
            """

            # Less stresful to read the code
            df, ld, p_d = self.basemap_dataframe, line_direction, perpendicular_direction

            #Measure the amount of perpendicular lines within line_direction
            dif_lines = abs(
                int(df[f"{perpendicular_direction}"].min() -
                    df[f"{perpendicular_direction}"].max())) + 1

            #Computing the coordinates of each
            utmx = np.linspace(
                float(df[(df[ld] == df[ld].min())
                         & (df[p_d] == df[p_d].min())]["utmx"].unique()),
                float(df[(df[ld] == df[ld].min())
                         & (df[p_d] == df[p_d].max())]["utmx"].unique()),
                num=dif_lines,
                endpoint=True)

            utmy = np.linspace(
                float(df[(df[ld] == df[ld].min())
                         & (df[p_d] == df[p_d].min())]["utmy"].unique()),
                float(df[(df[ld] == df[ld].min())
                         & (df[p_d] == df[p_d].max())]["utmy"].unique()),
                num=dif_lines,
                endpoint=True)

            #Array of perpendiculars
            array = np.arange(df[f"{p_d}"].min(), df[f"{p_d}"].max() + 1, 1)

            # Making dataframes to ease further calculations
            dlines = pd.DataFrame({
                ld: df[f"{ld}"].min(),
                p_d: array,
                "utmx": utmx,
                "utmy": utmy
            })

            return (dlines)

        def seismic_intersection(iline_df, xline_df, iline_number,
                                 xline_number):
            """
            NAME
            ----
                seismic_intersection
                
            DESCRIPTION
            -----------
                Computes the coordinates and tracf of the intersection between two seismic lines.

                The computation of the intersection uses vector differences.

            ARGUMENTS
            ---------
                iline_df : (Pandas)DataFrame
                    Coordinates of the traces within the first crossline.

                xline_df : (Pandas)DataFrame
                    Coordinates of the traces within the first inline.

                iline_number : int
                    Number of the chosen inline. 

                xline_number : int
                    Number of the chosen crossline. 

            RETURN
            ------
                list
                    List of tracf and coordinates of the intersection.
        
            """
            # Amount of CDP within crosslines
            dif_lines = abs(self.basemap_dataframe["xline"].max() -
                            self.basemap_dataframe["xline"].min()) + 1

            # tracf
            tracf = (iline_number -
                     self.basemap_dataframe["iline"].min()) * dif_lines + (
                         xline_number -
                         self.basemap_dataframe["xline"].min()) + 1

            # vector diferences. Formula utm = b - a + c
            tutmx = float(
                xline_df[xline_df["xline"] == xline_number]
                ["utmx"]) - xline_df["utmx"].iloc[0] + float(
                    iline_df[iline_df["iline"] == iline_number]["utmx"])
            tutmy = float(
                xline_df[xline_df["xline"] == xline_number]
                ["utmy"]) - xline_df["utmy"].iloc[0] + float(
                    iline_df[iline_df["iline"] == iline_number]["utmy"])

            return [int(tracf), tutmx, tutmy]

        df = self.basemap_dataframe
        # Assigning a variable for each dataframe in seismic_lines_dataframe
        ilines, xlines = seismic_lines_dataframe(
            df.keys()[1],
            df.keys()[0]), seismic_lines_dataframe(df.keys()[0],
                                                   df.keys()[1])

        # Extracting the intersection coordinates
        intersection = seismic_intersection(ilines, xlines, iline_number,
                                            xline_number)

        # Computing the second point to plot the seismic lines (By using vector differences)
        ## This can be refactored
        iutmx = float(xlines["utmx"].iloc[-1] - xlines["utmx"].iloc[0] +
                      ilines[ilines["iline"] == iline_number]["utmx"])
        iutmy = float(xlines["utmy"].iloc[-1] - xlines["utmy"].iloc[0] +
                      ilines[ilines["iline"] == iline_number]["utmy"])
        xutmx = float(ilines["utmx"].iloc[-1] - ilines["utmx"].iloc[0] +
                      xlines[xlines["xline"] == xline_number]["utmx"])
        xutmy = float(ilines["utmy"].iloc[-1] - ilines["utmy"].iloc[0] +
                      xlines[xlines["xline"] == xline_number]["utmy"])

        # hovers for lines and interception
        iline_hover = HoverTool(tooltips=[("Inline", f"{iline_number}")] +
                                self.hover_format)
        xline_hover = HoverTool(tooltips=[("Crossline", f"{xline_number}")] +
                                self.hover_format)
        int_hover = HoverTool(
            tooltips=[("Intersection", f"({iline_number}/{xline_number})")] +
            self.hover_format)

        #Updating hover attributes
        for item in [iline_hover, xline_hover, int_hover]:
            item._property_values.update(self.hover_attributes)

        # Plotting the Inline. Holoviews Curve element
        iline = hv.Curve(
            [(float(ilines[ilines["iline"] == iline_number]["utmx"]),
              float(ilines[ilines["iline"] == iline_number]["utmy"])),
             (iutmx, iutmy)],
            label="I-Line")

        # Plotting the Crossline. Holoviews Curve element
        xline = hv.Curve(
            [(float(xlines[xlines["xline"] == xline_number]["utmx"]),
              float(xlines[xlines["xline"] == xline_number]["utmy"])),
             (xutmx, xutmy)],
            label="C-Line")

        # Plot the intersection. Holovies Scatter element.
        intersection = hv.Scatter((intersection[1], intersection[2]),
                                  label="Intersection")

        # Adding the hover tool in to the plots
        iline.opts(line_width=2,
                   color="red",
                   tools=self.plot_tools + [iline_hover])
        xline.opts(line_width=2,
                   color="blue",
                   tools=self.plot_tools + [xline_hover])
        intersection.opts(size=7,
                          line_color="black",
                          line_width=2,
                          color="yellow",
                          tools=self.plot_tools + [int_hover])

        # Making the overlay of the seismic plot to deploy
        BasemapModule.seismic_lines = iline * xline * intersection
        return BasemapModule.seismic_lines

    def get_basemap(self):
        """
        NAME
        ----
            get_basemap
        
        DESCRIPTION
        -----------
            Constructs the basemap attribute and provides interactive methods to inspect the plotted 
            data.
            
            Merges polygon, wells and seismic_lines attributes into one object using Holoviews and 
            bokeh as backend. It also includes Panel's widgets and methods to add elements that ease 
            data management.
        
        ARGUMENTS
        ---------
            BasemapModule.basemap_dataframe : (Pandas)DataFrame
                Matrix compounded by the coordinates and lines of the seismic survey's corners.

            Survey.survey_name : str
                Name of the seismic survey.

        RETURN
        ------
            Panel Layout [Row]
                Container of the following indexed elements:
                    [0] WidgetBox
                    [0] Markdown for Survey.survey_name
                    [1] IntSlider for inline number selection
                    [2] IntSlider for crossline number selection
                    [3] Select for well selection
                    [1] basemap attribute
                     
        FUNCTIONS
        ---------
            basemap_plot(**kwargs)
                Constructs the basemap attribute.

            update_plot(**kwargs)
                Links Panel's selection widgets to the basemap attribute.
        
        """

        df = self.basemap_dataframe

        # Widgets
        iline_number = pn.widgets.IntSlider(name="Inline number",
                                            start=int(df["iline"].min()),
                                            end=int(df["iline"].max()),
                                            step=self.iline_step,
                                            value=int(df["iline"].min()))

        xline_number = pn.widgets.IntSlider(name="Crossline number",
                                            start=int(df["xline"].min()),
                                            end=int(df["xline"].max()),
                                            step=self.xline_step,
                                            value=int(df["xline"].min()))

        select_well = pn.widgets.Select(name="Select the well to inspect",
                                        options=["None"] +
                                        list(self.wells_dataframe["name"]),
                                        value="None")

        @pn.depends(iline_number.param.value, xline_number.param.value,
                    select_well.param.value)
        def basemap_plot(iline_number, xline_number, select_well):
            """
            NAME
            ----
                basemap_plot
            
            DESCRIPTION
            -----------
                Constructs the basemap attribute.

                Merges seismic survey related plots using Holoviews and bokeh as backend.
                
            ARGUMENTS
            ---------
                Arguments are given by Panel's widgets through the panel's depend decorator:

                iline_number : int
                    Number of the chosen inline.

                xline_number : int
                    Number of the chosen crossline.
                    
                select_well : str
                    Automatically gives well's line numbers when selected.

            RETURN
            ------
                basemap : Holviews element [Overlay] instance attribute
                    Combination of the plots: polygon, wells and seismic_lines.
            
            """
            #new attributes
            WiggleModule.inline_number = iline_number
            WiggleModule.crossline_number = xline_number

            # First element
            BasemapModule.polygon = BasemapModule.polygon_plot(self)
            # Second element
            BasemapModule.wells = BasemapModule.wells_plot(self)

            # Third element
            BasemapModule.seismic_lines = BasemapModule.seismic_line_plot(
                self, iline_number, xline_number)

            # Final Overlay
            BasemapModule.basemap = BasemapModule.polygon * BasemapModule.wells * BasemapModule.seismic_lines
            BasemapModule.basemap.opts(legend_position='top',
                                       height=600,
                                       width=600)

            return (BasemapModule.basemap)

        widgets = pn.WidgetBox(f"## {Survey.survey} Basemap", iline_number,
                               xline_number, select_well)

        def update_plot(event):
            """
            NAME
            ----
                update_plot
                
            DESCRIPTION
            -----------
                Links Panel's selection widgets to the basemap attribute.

                Modifies the target plot when a well is selected through Panel's selector widget.
                
                
            ARGUMENTS
            ---------
                event : str
                    Panel's selector widget value.
                     
            RETURN
            ------
                basemap : Holviews element [Overlay] instance attribute
                    Combination of the plots: polygon, wells and seismic_lines.
            
            """

            if select_well.value != "None":
                iline_number.value = int(
                    self.wells_dataframe["cdp_iline"].loc[str(
                        select_well.value)])
                xline_number.value = int(
                    self.wells_dataframe["cdp_xline"].loc[str(
                        select_well.value)])
                WiggleModule.inline_number = iline_number.value
                WiggleModule.crossline_number = xline_number.value

        select_well.param.watch(update_plot, 'value')

        return pn.Row(widgets, basemap_plot).servable()
コード例 #3
0
from .bokeh_plot_manager import BokehPlotManager
import math
from . import average_water_view
from rti_python.Post_Process.Average.AverageWaterColumn import AverageWaterColumn
import pandas as pd
import holoviews as hv
from holoviews import opts, dim, Palette
hv.extension('bokeh')
import panel as pn
pn.extension()
from bokeh.plotting import figure, ColumnDataSource
opts.defaults(
    opts.Bars(xrotation=45, tools=['hover']),
    opts.BoxWhisker(width=800, xrotation=30, box_fill_color=Palette('Category20')),
    opts.Curve(width=600, tools=['hover']),
    opts.GridSpace(shared_yaxis=True),
    opts.Scatter(width=800, height=400, color=Palette('Category20'), size=dim('growth')+5, tools=['hover']),
    opts.NdOverlay(legend_position='left'))


class AverageWaterVM(average_water_view.Ui_AvgWater, QWidget):

    increment_ens_sig = pyqtSignal(int)
    reset_avg_sig = pyqtSignal()
    avg_taken_sig = pyqtSignal()

    def __init__(self, parent, rti_config):
        average_water_view.Ui_AvgWater.__init__(self)
        QWidget.__init__(self, parent)
        self.setupUi(self)
        self.parent = parent
コード例 #4
0
ファイル: visual.py プロジェクト: pollackscience/pyviz_med
    def view(self,
             plane='axial',
             three_planes=False,
             image_size=300,
             dynamic=True,
             cmap='gray'):
        # imopts = {'tools': ['hover'], 'width': 400, 'height': 400, 'cmap': 'gray'}
        # imopts = {'tools': ['hover'], 'cmap': 'gray'}
        opts.defaults(
            opts.GridSpace(
                shared_xaxis=False,
                shared_yaxis=False,
                fontsize={
                    'title': 16,
                    'labels': 16,
                    'xticks': 12,
                    'yticks': 12
                },
            ),
            # opts.Image(cmap='gray', tools=['hover'], xaxis=None,
            #            yaxis=None, shared_axes=False),
            # opts.Overlay(tools=['hover']),
            # opts.NdOverlay(tools=['hover']),
            opts.Image(cmap=cmap, xaxis=None, yaxis=None, shared_axes=False),
        )

        self.is2d = False
        if 'z' not in self.ds.dims:
            self.is2d = True

        self.set_size(image_size)

        if self.is2d:
            plane == '2d'
            a1, a2 = 'x', 'y'
            pane_width = self.pane_width
            pane_height = self.pane_height
        else:
            if plane == 'axial':
                a1, a2, a3 = 'x', 'y', 'z'
                # invert = True
                pane_width = self.axial_width
                pane_height = self.axial_height
            elif plane == 'coronal':
                a1, a2, a3 = 'x', 'z', 'y'
                pane_width = self.coronal_width
                pane_height = self.coronal_height
                # invert = False
            elif plane == 'sagittal':
                a1, a2, a3 = 'y', 'z', 'x'
                # invert = False
                pane_width = self.sagittal_width
                pane_height = self.sagittal_height

        contrast_start_min = np.asscalar(
            self.ds.isel(subject_id=0, ).image.quantile(0.01).values) - 1e-6
        contrast_start_max = np.asscalar(
            self.ds.isel(subject_id=0, ).image.quantile(0.99).values) + 1e-6
        contrast_min = np.asscalar(
            self.ds.isel(subject_id=0).image.min().values)
        contrast_max = np.asscalar(
            self.ds.isel(subject_id=0).image.max().values)
        ctotal = contrast_max - contrast_min
        contrast_min -= ctotal * 0.1
        contrast_max += ctotal * 0.1

        cslider = pn.widgets.RangeSlider(start=contrast_min,
                                         end=contrast_max,
                                         value=(contrast_start_min,
                                                contrast_start_max),
                                         name='contrast')
        if 'overlay' in self.ds.data_vars:
            hv_ds_image = hv.Dataset(self.ds[['image', 'overlay']])
            # hv_ds_image = hv.Dataset(self.ds['image'])
            if self.verbose:
                print(hv_ds_image)
            hv_ds_overlay = hv.Dataset(self.ds['overlay'])
            if self.verbose:
                print(hv_ds_overlay)
            # tooltips = [
            #     ('(x,y)', '($x, $y)'),
            #     ('image', '@image'),
            #     ('overlay', '@overlay')
            # ]
            # hover = HoverTool(tooltips=tooltips)
            if self.verbose:
                print('overlay_max_calc')
            if self.is2d:
                first_subj_max = self.ds.isel(subject_id=0).overlay.max(
                    dim=['x', 'y', 'label']).compute()
                first_subj_min = self.ds.isel(subject_id=0).overlay.min(
                    dim=['x', 'y', 'label']).compute()
            else:
                first_subj_max = self.ds.isel(subject_id=0).overlay.max(
                    dim=['x', 'y', 'z', 'label']).compute()
                first_subj_min = self.ds.isel(subject_id=0).overlay.min(
                    dim=['x', 'y', 'z', 'label']).compute()
            if self.verbose:
                print('overlay_max_calc ready')
                print(first_subj_max)
            overlay_max = first_subj_max.max()
            alpha_slider = pn.widgets.FloatSlider(start=0,
                                                  end=1,
                                                  value=0.7,
                                                  name='overlay transparency')
            cmap_select = pn.widgets.Select(name='Overlay Colormap',
                                            options=['Discrete', 'Continuous'])

            if self.verbose:
                print('max thresh calc')
                print(first_subj_max.max())
            max_thresholds = first_subj_max.values
            if max_thresholds.size != 1:
                max_thresholds = sorted(set(max_thresholds))
            else:
                max_thresholds = [np.asscalar(max_thresholds)]
            # max_thresholds = sorted(list(set([first_subj.overlay.sel(overlay_label=i).values.max()
            #                         for i in first_subj.overlay_label])))
            if self.verbose:
                print('min thresh calc')
            min_thresholds = first_subj_min.values + 1e-6
            if min_thresholds.size != 1:
                min_thresholds = sorted(set(min_thresholds))
            else:
                min_thresholds = [np.asscalar(min_thresholds)]
            # min_thresholds = sorted(list(set(first_subj_min.min())))
            # min_thresholds = sorted(list(set([first_subj.sel(overlay_label=i).min()+1e-6 for i in
            #                        first_subj.overlay_label])))
            # ocslider = pn.widgets.DiscreteSlider(name='overlay max threshold',
            #                                      options=max_thresholds,
            #                                     value=max_thresholds[-1])
            if len(min_thresholds) == 1 and len(max_thresholds) == 1:
                thresh_toggle = 0
                oclim = (min_thresholds[0], max_thresholds[0])

            elif len(min_thresholds) > 1 and len(max_thresholds) == 1:
                thresh_toggle = 1
                ocslider_min = pn.widgets.DiscreteSlider(
                    name='overlay min threshold',
                    options=min_thresholds,
                    value=min_thresholds[-1])

                @pn.depends(ocslider_min)
                def oclim(value):
                    return (value, max_thresholds[0])

            elif len(min_thresholds) == 1 and len(max_thresholds) > 1:
                thresh_toggle = 2
                ocslider_max = pn.widgets.DiscreteSlider(
                    name='overlay max threshold',
                    options=max_thresholds,
                    value=max_thresholds[-1])

                @pn.depends(ocslider_max)
                def oclim(value):
                    return (min_thresholds[0], value)

            else:
                thresh_toggle = 3
                ocslider_min = pn.widgets.DiscreteSlider(
                    name='overlay min threshold',
                    options=min_thresholds,
                    value=min_thresholds[-1])
                ocslider_max = pn.widgets.DiscreteSlider(
                    name='overlay max threshold',
                    options=max_thresholds,
                    value=max_thresholds[-1])

                @pn.depends(ocslider_min, ocslider_max)
                def oclim(value_min, value_max):
                    return (value_min, value_max)

            if self.verbose:
                print(thresh_toggle)

            @pn.depends(cmap_select)
            def cmap_dict(value):
                d = {'Discrete': 'glasbey_hv', 'Continuous': 'viridis'}
                return d[value]

            # subj_viewer = SubjectViewer(ds=self.ds,
            #                             subject_id_sel=list(self.ds.subject_id.values))

            if self.is2d:
                gridspace = hv_ds_image.to(
                    hv.Image, [a1, a2],
                    vdims=['image', 'overlay'],
                    dynamic=dynamic).opts(
                        frame_width=pane_width,
                        frame_height=pane_height,
                        tools=['hover'],
                    ).apply.opts(clim=cslider.param.value)
                if self.verbose:
                    print(gridspace)
                gridspace *= hv_ds_overlay.to(
                    hv.Image, [a1, a2], vdims='overlay', dynamic=dynamic).opts(
                        cmap='glasbey_hv',
                        clipping_colors={
                            'min': 'transparent',
                            'NaN': 'transparent'
                        },
                    ).redim.range(overlay=(1e-6, overlay_max)).apply.opts(
                        alpha=alpha_slider.param.value,
                        cmap=cmap_dict,
                        clim=oclim)
                # print(gridspace)
                # print(gridspace)
                # gridspace = hv.DynamicMap(subj_viewer.load_subject).grid('label')
                gridspace = gridspace.layout('label')

            elif three_planes:
                # squish_height = int(max(image_size*(len(self.ds.z)/len(self.ds.x)), image_size/2))
                # gridspace = hv.GridSpace(kdims=['plane', 'label'], label=f'{self.subject_id}')
                gridspace = hv.GridSpace(kdims=['plane', 'label'])
                for mod in self.ds.label.values:
                    gridspace['axial', mod] = hv_ds_image.select(label=mod).to(
                        hv.Image, ['x', 'y'],
                        groupby=['subject_id', 'z'],
                        vdims='image',
                        dynamic=dynamic).opts(
                            frame_width=self.axial_width,
                            frame_height=self.axial_height).apply.opts(
                                clim=cslider.param.value)
                    gridspace['coronal', mod] = hv_ds_image.select(
                        label=mod).to(
                            hv.Image, ['x', 'z'],
                            groupby=['subject_id', 'y'],
                            vdims='image',
                            dynamic=dynamic).opts(
                                frame_width=self.coronal_width,
                                frame_height=self.coronal_height).apply.opts(
                                    clim=cslider.param.value)
                    gridspace['sagittal', mod] = hv_ds_image.select(
                        label=mod).to(
                            hv.Image, ['y', 'z'],
                            groupby=['subject_id', 'x'],
                            vdims='image',
                            dynamic=dynamic).opts(
                                frame_width=self.sagittal_width,
                                frame_height=self.sagittal_height).apply.opts(
                                    clim=cslider.param.value)

                    gridspace['axial',
                              mod] *= hv_ds_overlay.select(label=mod).to(
                                  hv.Image, ['x', 'y'],
                                  groupby=['subject_id', 'z', 'overlay_label'],
                                  vdims='overlay',
                                  dynamic=dynamic).opts(
                                      cmap='glasbey_hv',
                                      clipping_colors={
                                          'min': 'transparent',
                                          'NaN': 'transparent'
                                      },
                                  ).redim.range(
                                      overlay=(0.1, overlay_max)).apply.opts(
                                          alpha=alpha_slider.param.value,
                                          cmap=cmap_dict,
                                          clim=oclim)

                    gridspace['coronal',
                              mod] *= hv_ds_overlay.select(label=mod).to(
                                  hv.Image, ['x', 'z'],
                                  groupby=['subject_id', 'y', 'overlay_label'],
                                  vdims='overlay',
                                  dynamic=dynamic).opts(
                                      cmap='glasbey_hv',
                                      clipping_colors={
                                          'min': 'transparent',
                                          'NaN': 'transparent'
                                      },
                                  ).redim.range(
                                      overlay=(0.1, overlay_max)).apply.opts(
                                          alpha=alpha_slider.param.value,
                                          cmap=cmap_dict,
                                          clim=oclim)

                    gridspace['sagittal',
                              mod] *= hv_ds_overlay.select(label=mod).to(
                                  hv.Image, ['y', 'z'],
                                  groupby=['subject_id', 'x', 'overlay_label'],
                                  vdims='overlay',
                                  dynamic=dynamic).opts(
                                      cmap='glasbey_hv',
                                      clipping_colors={
                                          'min': 'transparent',
                                          'NaN': 'transparent'
                                      },
                                  ).redim.range(
                                      overlay=(0.1, overlay_max)).apply.opts(
                                          alpha=alpha_slider.param.value,
                                          cmap=cmap_dict,
                                          clim=oclim)

            else:
                # squish_height = int(max(image_size*(len(self.ds.z)/len(self.ds.x)), image_size/2))
                # gridspace = hv.GridSpace(kdims=['label'], label=f'{self.subject_id}')
                if self.verbose:
                    print('init gridspace')
                # gridspace = hv.GridSpace(kdims=['label'])
                # for mod in self.ds.label:
                #     gridspace[mod] = hv_ds_image.select(label=mod).to(
                #         hv.Image, [a1, a2], groupby=[a3], vdims='image',
                #         dynamic=dynamic).opts(frame_width=image_size, frame_height=image_size,
                #                            ).apply.opts(clim=cslider.param.value)
                #     gridspace[mod] *= hv_ds_overlay.select(label=mod).to(
                #         hv.Image, [a1, a2], groupby=[a3, 'overlay_label'], vdims='overlay',
                #         dynamic=dynamic).opts(
                #             cmap='glasbey_hv', clipping_colors={'min': 'transparent'},
                #         ).redim.range(overlay=(1e-6, overlay_max)).apply.opts(
                #             alpha=alpha_slider.param.value, cmap=cmap_dict, clim=oclim)
                #     gridspace[mod] = gridspace[mod].opts(tools=['hover'])
                #     print(gridspace[mod])

                gridspace = hv_ds_image.to(
                    hv.Image, [a1, a2],
                    vdims=['image', 'overlay'],
                    dynamic=dynamic).opts(
                        frame_width=pane_width,
                        frame_height=pane_height,
                        tools=['hover'],
                    ).apply.opts(clim=cslider.param.value)
                if self.verbose:
                    print(gridspace)
                gridspace *= hv_ds_overlay.to(
                    hv.Image, [a1, a2], vdims='overlay', dynamic=dynamic).opts(
                        cmap='glasbey_hv',
                        clipping_colors={
                            'min': 'transparent',
                            'NaN': 'transparent'
                        },
                    ).redim.range(overlay=(1e-6, overlay_max)).apply.opts(
                        alpha=alpha_slider.param.value,
                        cmap=cmap_dict,
                        clim=oclim)
                # print(gridspace)
                # print(gridspace)
                # gridspace = hv.DynamicMap(subj_viewer.load_subject).grid('label')
                gridspace = gridspace.layout('label')

        else:
            tooltips = [
                ('(x,y)', '($x, $y)'),
                ('image', '@image'),
            ]
            hover = HoverTool(tooltips=tooltips)
            hv_ds = hv.Dataset(self.ds['image'])
            if self.is2d:
                gridspace = hv.GridSpace(kdims=['label'])
                for mod in self.ds.label.values:
                    gridspace[mod] = hv_ds.select(label=mod).to(
                        hv.Image, [a1, a2],
                        groupby=['subject_id'],
                        vdims='image',
                        dynamic=dynamic).opts(
                            frame_width=pane_width,
                            frame_height=pane_height,
                            shared_axes=False,
                            tools=[hover],
                            axiswise=True,
                            # ).apply.opts(clim=cslider.param.value)
                        )
            elif three_planes:
                # squish_height = int(max(image_size*(len(self.ds.z)/len(self.ds.x)), image_size/2))
                # gridspace = hv.GridSpace(kdims=['plane', 'label'], label=f'{self.subject_id}')
                gridspace = hv.GridSpace(kdims=['plane', 'label'])
                for mod in self.ds.label.values:
                    gridspace['axial', mod] = hv_ds.select(label=mod).to(
                        hv.Image, ['x', 'y'],
                        groupby=['subject_id', 'z'],
                        vdims='image',
                        dynamic=dynamic).opts(frame_width=self.axial_width,
                                              frame_height=self.axial_height,
                                              invert_yaxis=False).apply.opts(
                                                  clim=cslider.param.value)
                    gridspace['coronal', mod] = hv_ds.select(label=mod).to(
                        hv.Image, ['x', 'z'],
                        groupby=['subject_id', 'y'],
                        vdims='image',
                        dynamic=dynamic).opts(
                            frame_width=self.coronal_width,
                            frame_height=self.coronal_height).apply.opts(
                                clim=cslider.param.value)
                    gridspace['sagittal', mod] = hv_ds.select(label=mod).to(
                        hv.Image, ['y', 'z'],
                        groupby=['subject_id', 'x'],
                        vdims='image',
                        dynamic=dynamic).opts(
                            frame_width=self.sagittal_width,
                            frame_height=self.sagittal_height).apply.opts(
                                clim=cslider.param.value)

            else:
                # squish_height = int(max(image_size*(len(self.ds.z)/len(self.ds.x)), image_size/2))
                # gridspace = hv.GridSpace(kdims=['label'], label=f'{self.subject_id}')
                gridspace = hv.GridSpace(kdims=['label'])
                for mod in self.ds.label.values:
                    gridspace[mod] = hv_ds.select(label=mod).to(
                        hv.Image, [a1, a2],
                        groupby=['subject_id', a3],
                        vdims='image',
                        dynamic=dynamic).opts(
                            frame_width=pane_width,
                            frame_height=pane_height,
                            shared_axes=False,
                            tools=[hover],
                        ).apply.opts(clim=cslider.param.value)

        pn_layout = pn.pane.HoloViews(gridspace)
        wb = pn_layout.widget_box
        wb.append(cslider)
        if 'overlay' in self.ds.data_vars:
            wb.append(alpha_slider)
            wb.append(cmap_select)
            if thresh_toggle in [2, 3]:
                wb.append(ocslider_max)
            if thresh_toggle in [1, 3]:
                wb.append(ocslider_min)
        return pn.Row(wb, pn_layout)