示例#1
0
 def test_colormapper_color_levels(self):
     cmap = process_cmap('viridis', provider='bokeh')
     img = Image(np.array([[0, 1], [2, 3]])).options(color_levels=5, cmap=cmap)
     plot = bokeh_renderer.get_plot(img)
     cmapper = plot.handles['color_mapper']
     self.assertEqual(len(cmapper.palette), 5)
     self.assertEqual(cmapper.palette, ['#440154', '#440255', '#440357', '#450558', '#45065A'])
示例#2
0
 def test_colormapper_color_levels(self):
     cmap = process_cmap('viridis', provider='bokeh')
     img = Image(np.array([[0, 1], [2, 3]])).options(color_levels=5, cmap=cmap)
     plot = bokeh_renderer.get_plot(img)
     cmapper = plot.handles['color_mapper']
     self.assertEqual(len(cmapper.palette), 5)
     self.assertEqual(cmapper.palette, ['#440154', '#440255', '#440357', '#450558', '#45065A'])
示例#3
0
    def __init__(self, **params):
        super(AdhView, self).__init__(**params)
        # initialize the boundary condition ui
        self.bc_ui = BoundaryConditionsUI(
            bound_cond=self.adh_mod.simulation.boundary_conditions)

        self.meshes = None

        if len(self.adh_mod.simulation.results.data_vars) != 0:
            # set default values
            self.param.selected_result.objects = self.adh_mod.simulation.results.data_vars
            try:
                self.selected_result = 'Depth'
            except:
                self.selected_result = set(
                    self.adh_mod.simulation.results.data_vars).pop()

            self.param.selected_times.objects = self.adh_mod.mesh.current_sim.results[
                self.selected_result].times.data
            self.selected_times = set(self.adh_mod.mesh.current_sim.results[
                self.selected_result].times.data).pop()

        # set default colormap
        self.cmap_opts.colormap = process_cmap('rainbow_r')
        # set default wmts
        self.adh_mod.wmts.source = gvts.tile_sources['EsriImagery']
示例#4
0
 def test_process_cmap_instance_mpl(self):
     try:
         from matplotlib.cm import get_cmap
     except:
         raise SkipError(
             "Matplotlib needed to test matplotlib colormap instances")
     cmap = get_cmap('Greys')
     colors = process_cmap(cmap, 3)
     self.assertEqual(colors, ['#ffffff', '#959595', '#000000'])
示例#5
0
class ColormapOpts(param.Parameterized):
    colormap = param.ObjectSelector(default=cc.coolwarm,
                                    objects={
                                        'Rainbow':
                                        cc.rainbow,
                                        'Rainbow Reverse':
                                        process_cmap('rainbow_r'),
                                        'Fire':
                                        cc.fire,
                                        'Fire Reverse':
                                        process_cmap('fire_r'),
                                        'Gray':
                                        cc.gray,
                                        'Gray Reverse':
                                        process_cmap('gray_r'),
                                        'CoolWarm':
                                        cc.coolwarm,
                                        'WarmCool':
                                        process_cmap('coolwarm_r')
                                    })
示例#6
0
 def test_hex_tiles_opts(self):
     from holoviews.plotting.util import process_cmap
     tiles = HexTiles([(0, 0), (0.5, 0.5), (-0.5, -0.5), (-0.4, -0.4)])
     plot = mpl_renderer.get_plot(tiles)
     args, style, _ = plot.get_data(tiles, {}, {})
     self.assertEqual(args[0], tiles['x'])
     self.assertEqual(args[1], tiles['y'])
     self.assertEqual(args[2], np.ones(4))
     cmap = style.pop('cmap')
     self.assertEqual(style, {'reduce_C_function': np.sum,
                              'vmin': None, 'vmax': None,
                              'xscale': 'linear', 'yscale': 'linear',
                              'gridsize': 50, 'mincnt': None})
     self.assertEqual(cmap.colors, process_cmap('viridis'))
示例#7
0
def _updateConsensusRows(kmerDf, newClass=None):
    cond = (kmerDf['id'].str.match('C[1-6]'))

    numRows = len(kmerDf.loc[cond])
    colors = process_cmap('glasbey_dark')[:numRows]

    kmerDf = _updateClass(kmerDf, cond, newClass)
    kmerDf.loc[cond, 'bSize'] = 10
    kmerDf.loc[cond, 'mSize'] = 100
    if (newClass is None):
        kmerDf.loc[cond, 'color'] = colors
    else:
        kmerDf.loc[cond, 'color'] = '#d62728'

    return kmerDf
示例#8
0
def _updateAusRows(kmerDf, newClass=None):
    cond = ((kmerDf['id'].str.contains('Australia'))
            | (kmerDf['id'].str.contains('Sydney'))
            | (kmerDf['dupId'].str.contains('Australia'))
            | (kmerDf['dupId'].str.contains('Sydney')))

    numRows = len(kmerDf.loc[cond])
    colors = process_cmap('glasbey_dark')[:numRows]

    kmerDf = _updateClass(kmerDf, cond, newClass)
    kmerDf.loc[cond, 'bSize'] = 10
    kmerDf.loc[cond, 'mSize'] = 100
    if (newClass is None):
        kmerDf.loc[cond, 'color'] = colors

    else:
        kmerDf.loc[cond, 'color'] = '#1f77b4'

    return kmerDf
示例#9
0
 def test_hex_tiles_opts(self):
     from holoviews.plotting.util import process_cmap
     tiles = HexTiles([(0, 0), (0.5, 0.5), (-0.5, -0.5), (-0.4, -0.4)])
     plot = mpl_renderer.get_plot(tiles)
     args, style, _ = plot.get_data(tiles, {}, {})
     self.assertEqual(args[0], tiles['x'])
     self.assertEqual(args[1], tiles['y'])
     self.assertEqual(args[2], np.ones(4))
     cmap = style.pop('cmap')
     self.assertEqual(
         style, {
             'reduce_C_function': np.sum,
             'vmin': None,
             'vmax': None,
             'xscale': 'linear',
             'yscale': 'linear',
             'gridsize': 50,
             'mincnt': None
         })
     self.assertEqual(cmap.colors, process_cmap('viridis'))
示例#10
0
def _updateRecombinationRows(kmerDf, newClass=None):
    cond = ((kmerDf['id'].str.contains('Korea/KCDC05'))
            | (kmerDf['id'].str.contains('Chongqing/IVDC-CQ-001'))
            | (kmerDf['id'].str.contains('Italy/INMI1-cs'))
            | (kmerDf['dupId'].str.contains('Korea/KCDC05'))
            | (kmerDf['dupId'].str.contains('Chongqing/IVDC-CQ-001'))
            | (kmerDf['dupId'].str.contains('Italy/INMI1-cs')))

    numRows = len(kmerDf.loc[cond])
    colors = set(process_cmap('glasbey_warm'))[:numRows]

    kmerDf = _updateClass(kmerDf, cond, newClass)
    kmerDf.loc[cond, 'bSize'] = 10
    kmerDf.loc[cond, 'mSize'] = 100
    if (newClass is None):
        kmerDf.loc[cond, 'color'] = colors

    else:
        kmerDf.loc[cond, 'color'] = '#ce6dbd'

    return kmerDf
示例#11
0
def _updateAnimalModelRows(kmerDf, newClass=None):
    cond = ((kmerDf['id'].str.contains('Australia/VIC01'))
            | (kmerDf['id'].str.contains('BetaCoV_France_IDF0372_2020_C2'))
            | (kmerDf['id'].str.contains('Canada/ON-VIDO-01'))
            | (kmerDf['id'].str.contains('Germany/BavPat1'))
            | (kmerDf['id'].str.contains('USA/WA1'))
            | (kmerDf['dupId'].str.contains('Australia/VIC01'))
            | (kmerDf['dupId'].str.contains('BetaCoV_France_IDF0372_2020_C2'))
            | (kmerDf['dupId'].str.contains('Canada/ON-VIDO-01'))
            | (kmerDf['dupId'].str.contains('Germany/BavPat1'))
            | (kmerDf['dupId'].str.contains('USA/WA1')))

    numRows = len(kmerDf.loc[cond])
    colors = set(process_cmap('glasbey_cool'))[:numRows]

    kmerDf = _updateClass(kmerDf, cond, newClass)
    kmerDf.loc[cond, 'bSize'] = 10
    kmerDf.loc[cond, 'mSize'] = 100
    if (newClass is None):
        kmerDf.loc[cond, 'color'] = colors
    else:
        kmerDf.loc[cond, 'color'] = '#2ca02c'

    return kmerDf
 def test_bokeh_colormap_fire_r(self):
     colors = process_cmap('fire_r', 3, provider='bokeh')
     self.assertEqual(colors, ['#ffffff', '#ed1400', '#000000'])
 def test_mpl_colormap_fire_r(self):
     colors = process_cmap('fire_r', 3, provider='matplotlib')
     self.assertEqual(colors, ['#ffffff', '#eb1300', '#000000'])
示例#14
0
 def test_process_cmap_bokeh(self):
     colors = process_cmap('Category20', 3)
     self.assertEqual(colors, ['#1f77b4', '#aec7e8', '#ff7f0e'])
示例#15
0
hv.output(dpi=300, size=100)
doc = curdoc()  # DOC for Bokeh Objects

# DATA SOURCE
source = 'tcp://10.253.0.142:6666'  # LIVE
#~ source = 'tcp://127.0.0.1:8010' # emulated live
tof_in_stream = True
pnCCD_in_stream = True
gmd_in_stream = True

two_monitors = False

img_downscale = 30
#~ colormap_pnCCD = 'Plasma'
#~ colormap_pnCCD = process_cmap('rainbow',provider='colorcet')
colormap_pnCCD = process_cmap('jet', provider='matplotlib')
pnCCD_color_lower_z_lim = 0.02  #0.02
pnCCD_color_upper_z_lim = 16  #5
pnCCDavg_color_lower_z_lim = 0.005  #0.005
pnCCDavg_color_upper_z_lim = 16  #2

makeBigData_stop = False
# DATA CONFIG
N_datapts = 31000  # total number of TOF datapoints that are visualized
start_tof = 59000  # index of first TOF datapoint considered
single_photon_adu = 250
background_pnCCD = np.zeros(shape=(1024, 1024))
background_max_pnCCD = np.zeros(shape=(1024, 1024))
dark_pnCCD = np.zeros(shape=(1024, 1024))
get_background_pnCCD = False
get_background_max_pnCCD = False
示例#16
0
 def test_process_cmap_mpl(self):
     colors = process_cmap('Greys', 3)
     self.assertEqual(colors, ['#ffffff', '#959595', '#000000'])
示例#17
0
 def test_process_cmap_cycle(self):
     colors = process_cmap(Cycle(values=['#ffffff', '#959595', '#000000']), 4)
     self.assertEqual(colors, ['#ffffff', '#959595', '#000000', '#ffffff'])
示例#18
0
 def test_mpl_colormap_instance(self):
     from matplotlib.cm import get_cmap
     cmap = get_cmap('Greys')
     colors = process_cmap(cmap, 3)
     self.assertEqual(colors, ['#ffffff', '#959595', '#000000'])
示例#19
0
 def test_process_cmap_invalid_type(self):
     with self.assertRaises(TypeError):
         process_cmap({'A', 'B', 'C'}, 3)
示例#20
0
    def view(self):
        if self.lsd is None:
            return panel.pane.Markdown("No data selected.")
        try:
            if self.intercylinder_only:
                name = "ringmap_intercyl"
            else:
                name = "ringmap"
            container = self.data.load_file(self.revision, self.lsd, name)
        except DataError as err:
            return panel.pane.Markdown(
                f"Error: {str(err)}. Please report this problem."
            )

        # Index map for ra (x-axis)
        index_map_ra = container.index_map["ra"]
        axis_name_ra = "RA [degrees]"

        # Index map for sin(ZA)/sin(theta) (y-axis)
        index_map_el = container.index_map["el"]
        axis_name_el = "sin(\u03B8)"

        # Apply data selections
        sel_beam = np.where(container.index_map["beam"] == self.beam)[0]
        sel_freq = np.where(
            [f[0] for f in container.index_map["freq"]] == self.frequency
        )[0]
        if self.polarization == self.mean_pol_text:
            sel_pol = np.where(
                (container.index_map["pol"] == "XX")
                | (container.index_map["pol"] == "YY")
            )[0]
            rmap = np.squeeze(container.map[sel_beam, sel_pol, sel_freq])
            rmap = np.nanmean(rmap, axis=0)
        else:
            sel_pol = np.where(container.index_map["pol"] == self.polarization)[0]
            rmap = np.squeeze(container.map[sel_beam, sel_pol, sel_freq])

        if self.flag_mask:
            rmap = np.where(self._flags_mask(container.index_map["ra"]), np.nan, rmap)

        if self.weight_mask:
            try:
                rms = np.squeeze(container.rms[sel_pol, sel_freq])
            except IndexError:
                logger.error(
                    f"rms dataset of ringmap file for rev {self.revision} lsd "
                    f"{self.lsd} is missing [{sel_pol}, {sel_freq}] (polarization, "
                    f"frequency). rms has shape {container.rms.shape}"
                )
                self.weight_mask = False
            else:
                rmap = np.where(self._weights_mask(rms), np.nan, rmap)

        # Set flagged data to nan
        rmap = np.where(rmap == 0, np.nan, rmap)

        if self.crosstalk_removal:
            # The mean of an all-nan slice (masked?) is nan. We don't need a warning about that.
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", r"All-NaN slice encountered")
                rmap -= np.nanmedian(rmap, axis=0)

        if self.template_subtraction:
            try:
                rm_stack = self.data.load_file_from_path(
                    self._stack_path, ccontainers.RingMap
                )
            except DataError as err:
                return panel.pane.Markdown(
                    f"Error: {str(err)}. Please report this problem."
                )

            # The stack file has all polarizations, so we can't reuse sel_pol
            if self.polarization == self.mean_pol_text:
                stack_sel_pol = np.where(
                    (rm_stack.index_map["pol"] == "XX")
                    | (rm_stack.index_map["pol"] == "YY")
                )[0]
            else:
                stack_sel_pol = np.where(
                    rm_stack.index_map["pol"] == self.polarization
                )[0]

            try:
                rm_stack = np.squeeze(rm_stack.map[sel_beam, stack_sel_pol, sel_freq])
            except IndexError as err:
                logger.error(
                    f"map dataset of ringmap stack file "
                    f"is missing [{sel_beam}, {stack_sel_pol}, {sel_freq}] (beam, polarization, "
                    f"frequency). map has shape {rm_stack.map.shape}:\n{err}"
                )
                self.template_subtraction = False
            else:
                if self.polarization == self.mean_pol_text:
                    rm_stack = np.nanmean(rm_stack, axis=0)

                # FIXME: this is a hack. remove when rinmap stack file fixed.
                rmap -= rm_stack.reshape(rm_stack.shape[0], -1, 2).mean(axis=-1)

        if self.transpose:
            rmap = rmap.T
            index_x = index_map_ra
            index_y = index_map_el
            axis_names = [axis_name_ra, axis_name_el]
            xlim, ylim = self.ylim, self.xlim
        else:
            index_x = index_map_el
            index_y = index_map_ra
            axis_names = [axis_name_el, axis_name_ra]
            xlim, ylim = self.xlim, self.ylim

        img = hv.Image(
            (index_x, index_y, rmap),
            datatype=["image", "grid"],
            kdims=axis_names,
        ).opts(
            clim=self.colormap_range,
            logz=self.logarithmic_colorscale,
            cmap=process_cmap("inferno", provider="matplotlib"),
            colorbar=True,
            xlim=xlim,
            ylim=ylim,
        )

        if self.serverside_rendering is not None:
            # set colormap
            cmap_inferno = copy.copy(matplotlib_cm.get_cmap("inferno"))
            cmap_inferno.set_under("black")
            cmap_inferno.set_bad("lightgray")

            # Set z-axis normalization (other possible values are 'eq_hist', 'cbrt').
            if self.logarithmic_colorscale:
                normalization = "log"
            else:
                normalization = "linear"

            # datashade/rasterize the image
            img = self.serverside_rendering(
                img,
                cmap=cmap_inferno,
                precompute=True,
                x_range=xlim,
                y_range=ylim,
                normalization=normalization,
            )

        if self.mark_moon:
            # Put a ring around the location of the moon if it transits on this day
            eph = skyfield_wrapper.ephemeris

            # Start and end times of the CSD
            st = csd_to_unix(self.lsd.lsd)
            et = csd_to_unix(self.lsd.lsd + 1)

            moon_time, moon_dec = chime.transit_times(
                eph["moon"], st, et, return_dec=True
            )

            if len(moon_time):
                lunar_transit = unix_to_csd(moon_time[0])
                lunar_dec = moon_dec[0]
                lunar_ra = (lunar_transit % 1) * 360.0
                lunar_za = np.sin(np.radians(lunar_dec - 49.0))
                if self.transpose:
                    img *= hv.Ellipse(lunar_ra, lunar_za, (5.5, 0.15))
                else:
                    img *= hv.Ellipse(lunar_za, lunar_ra, (0.04, 21))

        if self.mark_day_time:
            # Calculate the sun rise/set times on this sidereal day

            # Start and end times of the CSD
            start_time = csd_to_unix(self.lsd.lsd)
            end_time = csd_to_unix(self.lsd.lsd + 1)

            times, rises = chime.rise_set_times(
                skyfield_wrapper.ephemeris["sun"],
                start_time,
                end_time,
                diameter=-10,
            )
            sun_rise = 0
            sun_set = 0
            for t, r in zip(times, rises):
                if r:
                    sun_rise = (unix_to_csd(t) % 1) * 360
                else:
                    sun_set = (unix_to_csd(t) % 1) * 360

            # Highlight the day time data
            opts = {
                "color": "grey",
                "alpha": 0.5,
                "line_width": 1,
                "line_color": "black",
                "line_dash": "dashed",
            }
            if self.transpose:
                if sun_rise < sun_set:
                    img *= hv.VSpan(sun_rise, sun_set).opts(**opts)
                else:
                    img *= hv.VSpan(self.ylim[0], sun_set).opts(**opts)
                    img *= hv.VSpan(sun_rise, self.ylim[1]).opts(**opts)

            else:
                if sun_rise < sun_set:
                    img *= hv.HSpan(sun_rise, sun_set).opts(**opts)
                else:
                    img *= hv.HSpan(self.ylim[0], sun_set).opts(**opts)
                    img *= hv.HSpan(sun_rise, self.ylim[1]).opts(**opts)

        img.opts(
            # Fix height, but make width responsive
            height=self.height,
            responsive=True,
            shared_axes=True,
            bgcolor="lightgray",
        )

        return panel.Row(img, width_policy="max")
示例#21
0
    def __call__(self, dset, **params):
        self.p = ParamOverrides(self, params)
        if self.p.xdim not in dset.dimensions():
            raise ValueError("{} not in Dataset.".format(self.p.xdim))
        if self.p.ydim not in dset.dimensions():
            raise ValueError("{} not in Dataset.".format(self.p.ydim))
        if ("ra" not in dset.dimensions()) or ("dec" not in dset.dimensions()):
            raise ValueError("ra and/or dec not in Dataset.")

        # Compute sampling
        ra_range = (ra0, ra1) = dset.range("ra")
        if self.p.ra_sampling:
            ra_sampling = (ra1 - ra0) / self.p.x_sampling
        else:
            ra_sampling = None

        dec_range = (dec0, dec1) = dset.range("dec")
        if self.p.dec_sampling:
            dec_sampling = (dec1 - dec0) / self.p.y_sampling
        else:
            dec_sampling = None

        x_range = (x0, x1) = dset.range(self.p.xdim)
        if self.p.x_sampling:
            x_sampling = (x1 - x0) / self.p.x_sampling
        else:
            x_sampling = None

        y_range = (y0, y1) = dset.range(self.p.ydim)
        if self.p.y_sampling:
            y_sampling = (y1 - y0) / self.p.y_sampling
        else:
            y_sampling = None

        # Set up scatter plot
        scatter_range = RangeXY()
        if self.p.scatter_range_stream:

            def redim_scatter(dset, x_range, y_range):
                ranges = {}
                if x_range and all(isfinite(v) for v in x_range):
                    ranges[self.p.xdim] = x_range
                if y_range and all(isfinite(v) for v in x_range):
                    ranges[self.p.ydim] = y_range
                return dset.redim.range(**ranges) if ranges else dset

            dset_scatter = dset.apply(redim_scatter,
                                      streams=[self.p.scatter_range_stream])
            link_streams(self.p.scatter_range_stream, scatter_range)
        else:
            dset_scatter = dset
        scatter_pts = dset_scatter.apply(filterpoints,
                                         streams=[self.p.filter_stream],
                                         xdim=self.p.xdim,
                                         ydim=self.p.ydim)
        scatter_streams = [scatter_range, PlotSize()]
        scatter_rasterize = rasterize.instance(streams=scatter_streams,
                                               x_sampling=x_sampling,
                                               y_sampling=y_sampling)
        cmap = (process_cmap(self.p.scatter_cmap)[:250]
                if self.p.scatter_cmap == "fire" else self.p.scatter_cmap)
        scatter_rasterized = apply_when(
            scatter_pts,
            operation=scatter_rasterize,
            predicate=lambda pts: len(pts) > self.p.max_points
        ).opts(
            opts.Image(clim=(1, np.nan),
                       clipping_colors={"min": "transparent"},
                       cmap=cmap),
            opts.Points(clim=(1, np.nan),
                        clipping_colors={"min": "transparent"},
                        cmap=cmap),
            opts.Overlay(
                hooks=[partial(reset_hook, x_range=x_range, y_range=y_range)]),
        )

        # Set up sky plot
        sky_range = RangeXY()
        if self.p.sky_range_stream:

            def redim_sky(dset, x_range, y_range):
                ranges = {}
                if x_range and all(isfinite(v) for v in x_range):
                    ranges["ra"] = x_range
                if y_range and all(isfinite(v) for v in x_range):
                    ranges["dec"] = y_range
                return dset.redim.range(**ranges) if ranges else dset

            dset_sky = dset.apply(redim_sky, streams=[self.p.sky_range_stream])
            link_streams(self.p.sky_range_stream, sky_range)
        else:
            dset_sky = dset
        sky_pts = dset_sky.apply(filterpoints,
                                 xdim="ra",
                                 ydim="dec",
                                 set_title=False,
                                 streams=[self.p.filter_stream])
        skyplot_streams = [sky_range, PlotSize()]
        sky_rasterize = rasterize.instance(
            aggregator=ds.mean(self.p.ydim),
            streams=skyplot_streams,
            x_sampling=ra_sampling,
            y_sampling=dec_sampling,
        )
        sky_rasterized = apply_when(
            sky_pts,
            operation=sky_rasterize,
            predicate=lambda pts: len(pts) > self.p.max_points).opts(
                opts.Image(bgcolor="black",
                           cmap=self.p.sky_cmap,
                           symmetric=True),
                opts.Points(bgcolor="black",
                            cmap=self.p.sky_cmap,
                            symmetric=True),
                opts.Overlay(hooks=[
                    partial(reset_hook, x_range=ra_range, y_range=dec_range)
                ]),
            )

        # Set up BoundsXY streams to listen to box_select events and notify FilterStream
        scatter_select = BoundsXY(source=scatter_pts)
        scatter_notifier = partial(notify_stream,
                                   filter_stream=self.p.filter_stream,
                                   xdim=self.p.xdim,
                                   ydim=self.p.ydim)
        scatter_select.add_subscriber(scatter_notifier)

        sky_select = BoundsXY(source=sky_pts)
        sky_notifier = partial(notify_stream,
                               filter_stream=self.p.filter_stream,
                               xdim="ra",
                               ydim="dec")
        sky_select.add_subscriber(sky_notifier)

        # Reset
        reset = PlotReset(source=sky_pts)
        reset.add_subscriber(
            partial(reset_stream, self.p.filter_stream,
                    [self.p.sky_range_stream, self.p.scatter_range_stream]))

        raw_scatterpts = filterpoints(dset, xdim=self.p.xdim, ydim=self.p.ydim)
        raw_scatter = datashade(
            raw_scatterpts,
            cmap=list(Greys9[::-1][:5]),
            streams=scatter_streams,
            x_sampling=x_sampling,
            y_sampling=y_sampling,
        )
        scatter_p = raw_scatter * scatter_rasterized

        if self.p.show_rawsky:
            raw_skypts = filterpoints(dset, xdim=self.p.xdim, ydim=self.p.ydim)
            raw_sky = datashade(
                rawskypts,
                cmap=list(Greys9[::-1][:5]),
                streams=skyplot_streams,
                x_sampling=ra_sampling,
                y_sampling=dec_sampling,
            )
            sky_p = raw_sky * sky_rasterized
        else:
            sky_p = sky_rasterized

        if self.p.show_table:
            table = dset.apply(summary_table,
                               ydim=self.p.ydim,
                               streams=[self.p.filter_stream])
            table = table.opts()
            layout = table + scatter_p + sky_p
        else:
            layout = (scatter_p + sky_p).opts(sizing_mode="stretch_width")

        return layout.opts(
            opts.Image(colorbar=True,
                       responsive=True,
                       tools=["box_select", "hover"]),
            opts.Layout(sizing_mode="stretch_width"),
            opts.Points(color=self.p.ydim, tools=["hover"]),
            opts.RGB(alpha=0.5),
            opts.Table(width=200),
        )
 def hex_list(self):
     if self._name is not None:
         return process_cmap(self._name)
     else:
         return None
示例#23
0
 def test_process_cmap_list_cycle(self):
     colors = process_cmap(['#ffffff', '#959595', '#000000'], 4)
     self.assertEqual(colors, ['#ffffff', '#959595', '#000000', '#ffffff'])
示例#24
0
def main(args=None):

    args = parse_arguments().parse_args(args)
    # if args.threads <= 4:
    #     log.error('')
    #     exit(1)
    outputFolder = os.path.dirname(os.path.abspath(args.outFileName)) + '/'

    raw_file_name = os.path.splitext(os.path.basename(args.outFileName))[0]

    if args.numberOfNearestNeighbors is None:
        cooler_obj = cooler.Cooler(args.matrix)
        args.numberOfNearestNeighbors = int(cooler_obj.info['ncells'])
    if args.cell_coloring_type:
        cell_name_cell_type_dict = {}

        cell_type_color_dict = {}
        color_cell_type_dict = {}
        cell_type_counter = 0
        with open(args.cell_coloring_type, 'r') as file:
            for i, line in enumerate(file.readlines()):
                line = line.strip()
                try:
                    cell_name, cell_type = line.split('\t')
                except Exception:
                    cell_name, cell_type = line.split('    ')
                cell_name_cell_type_dict[cell_name] = cell_type
                if cell_type not in cell_type_color_dict:
                    cell_type_color_dict[cell_type] = cell_type_counter
                    color_cell_type_dict[cell_type_counter] = cell_type
                    cell_type_counter += 1

    if args.cell_coloring_batch:
        cell_name_cell_type_dict_batch = {}

        cell_type_color_dict_batch = {}
        color_cell_type_dict_batch = {}
        cell_type_counter_batch = 0
        with open(args.cell_coloring_batch, 'r') as file:
            for i, line in enumerate(file.readlines()):
                line = line.strip()
                try:
                    cell_name, cell_type = line.split('\t')
                except Exception:
                    cell_name, cell_type = line.split('    ')
                cell_name_cell_type_dict_batch[cell_name] = cell_type
                if cell_type not in cell_type_color_dict_batch:
                    cell_type_color_dict_batch[cell_type] = cell_type_counter_batch
                    color_cell_type_dict_batch[cell_type_counter_batch] = cell_type
                    cell_type_counter_batch += 1

    if args.clusterMethod == 'spectral':
        cluster_object = SpectralClustering(n_clusters=args.numberOfClusters, affinity='nearest_neighbors', n_jobs=args.threads, random_state=0)
    elif args.clusterMethod == 'kmeans':
        cluster_object = KMeans(n_clusters=args.numberOfClusters, random_state=0, n_jobs=args.threads, precompute_distances=True)
    elif args.clusterMethod.startswith('agglomerative'):
        for linkage in ['ward', 'complete', 'average', 'single']:
            if linkage in args.clusterMethod:
                cluster_object = AgglomerativeClustering(n_clusters=args.numberOfClusters, linkage=linkage)
                break
    elif args.clusterMethod == 'birch':
        cluster_object = Birch(n_clusters=args.numberOfClusters)
    else:
        log.error('No valid cluster method given: {}'.format(args.clusterMethod))

    umap_params_dict = {}

    if not args.noUMAP:
        for param in vars(args):
            if 'umap_' in param:
                umap_params_dict[param] = vars(args)[param]
        umap_params_dict['umap_random'] = 42
    # log.debug(umap_params_dict)

    if args.saveMemory:
        matrices_list = cell_name_list(args.matrix)
        max_nnz = 0
        for matrix in matrices_list:
            cooler_obj = cooler.Cooler(args.matrix + '::' + matrix)
            nnz = cooler_obj.info['nnz']
            if max_nnz < nnz:
                max_nnz = nnz
        minHash_object = None
        matricesPerRun = int(len(matrices_list) * args.shareOfMatrixToBeTransferred)
        if matricesPerRun < 1:
            matricesPerRun = 1
        chromosome_indices = None
        if args.intraChromosomalContactsOnly:
            cooler_obj = cooler.Cooler(args.matrix + '::' + matrices_list[0])
            binsDataFrame = cooler_obj.bins()[:]
            chromosome_indices = {}
            for chromosome in cooler_obj.chromnames:
                chromosome_indices[chromosome] = np.array(binsDataFrame.index[binsDataFrame['chrom'] == chromosome].tolist())

        for j, i in enumerate(range(0, len(matrices_list), matricesPerRun)):
            if i < len(matrices_list) - 1:
                matrices_share = matrices_list[i:i + matricesPerRun]
            else:
                matrices_share = matrices_list[i:]
            neighborhood_matrix, matrices_list_share = open_and_store_matrix(args.matrix, matrices_share, 0, len(matrices_share),
                                                                             args.chromosomes, args.intraChromosomalContactsOnly, chromosome_indices)
            if minHash_object is None:
                minHash_object = MinHash(n_neighbors=args.numberOfNearestNeighbors, number_of_hash_functions=args.numberOfHashFunctions, number_of_cores=args.threads,
                                         shingle_size=0, fast=args.euclideanModeMinHash, maxFeatures=int(max_nnz), absolute_numbers=False)

            if j == 0:
                minHash_object.fit(neighborhood_matrix)
            else:
                minHash_object.partial_fit(X=neighborhood_matrix)

        precomputed_graph = minHash_object.kneighbors_graph(mode='distance')
        precomputed_graph = np.nan_to_num(precomputed_graph)
        precomputed_graph.data[np.isinf(precomputed_graph.data)] = 0
        if not args.noPCA:

            pca = PCA(n_components=min(precomputed_graph.shape) - 1)
            precomputed_graph = np.nan_to_num(precomputed_graph.todense())
            precomputed_graph[np.isinf(precomputed_graph)] = 0
            precomputed_graph = pca.fit_transform(precomputed_graph)

            if args.dimensionsPCA:
                args.dimensionsPCA = min(args.dimensionsPCA, precomputed_graph.shape[0])
                precomputed_graph = precomputed_graph[:, :args.dimensionsPCA]
                # cluster_object.fit(precomputed_graph[:, :args.dimensionsPCA])
        if not args.noUMAP:

            if umap_params_dict is None:
                reducer = umap.UMAP()
            else:
                reducer = umap.UMAP(n_neighbors=umap_params_dict['umap_n_neighbors'], n_components=umap_params_dict['umap_n_components'], metric=umap_params_dict['umap_metric'],
                                    n_epochs=umap_params_dict['umap_n_epochs'],
                                    learning_rate=umap_params_dict['umap_learning_rate'], init=umap_params_dict['umap_init'], min_dist=umap_params_dict['umap_min_dist'], spread=umap_params_dict['umap_spread'],
                                    set_op_mix_ratio=umap_params_dict['umap_set_op_mix_ratio'], local_connectivity=umap_params_dict['umap_local_connectivity'],
                                    repulsion_strength=umap_params_dict['umap_repulsion_strength'], negative_sample_rate=umap_params_dict['umap_negative_sample_rate'], transform_queue_size=umap_params_dict['umap_transform_queue_size'],
                                    a=umap_params_dict['umap_a'], b=umap_params_dict['umap_b'], angular_rp_forest=umap_params_dict['umap_angular_rp_forest'],
                                    target_n_neighbors=umap_params_dict['umap_target_n_neighbors'], target_metric=umap_params_dict['umap_target_metric'],
                                    target_weight=umap_params_dict['umap_target_weight'], random_state=umap_params_dict['umap_random'],
                                    force_approximation_algorithm=umap_params_dict['umap_force_approximation_algorithm'], verbose=umap_params_dict['umap_verbose'], unique=umap_params_dict['umap_unique'])
            precomputed_graph = reducer.fit_transform(precomputed_graph)
        precomputed_graph = np.nan_to_num(precomputed_graph)
        precomputed_graph[np.isinf(precomputed_graph)] = 0

        try:
            cluster_object.fit(precomputed_graph)
        except Exception:
            cluster_object.fit(precomputed_graph.todense())

        minHashClustering = MinHashClustering(minHashObject=minHash_object, clusteringObject=cluster_object)
        minHashClustering._precomputed_graph = precomputed_graph

    else:
        neighborhood_matrix, matrices_list = create_csr_matrix_all_cells(args.matrix, args.threads, args.chromosomes, outputFolder, raw_file_name, args.intraChromosomalContactsOnly, pDistance=args.distance)

        if args.saveIntermediateRawMatrix:
            save_npz(args.saveIntermediateRawMatrix, neighborhood_matrix)

    if not args.saveMemory:
        minHash_object = MinHash(n_neighbors=args.numberOfNearestNeighbors, number_of_hash_functions=args.numberOfHashFunctions, number_of_cores=args.threads,
                                 shingle_size=5, fast=args.euclideanModeMinHash, maxFeatures=int(max(neighborhood_matrix.getnnz(1))), absolute_numbers=False, max_bin_size=100000,
                                 minimal_blocks_in_common=100, excess_factor=1, prune_inverse_index=False)
        minHashClustering = MinHashClustering(minHashObject=minHash_object, clusteringObject=cluster_object)
        minHashClustering.fit(X=neighborhood_matrix, pSaveMemory=args.shareOfMatrixToBeTransferred, pPca=(not args.noPCA), pPcaDimensions=args.dimensionsPCA, pUmap=(not args.noUMAP), pUmapDict=umap_params_dict)

    if args.noPCA and args.noUMAP:
        mask = np.isnan(minHashClustering._precomputed_graph.data)
        minHashClustering._precomputed_graph.data[mask] = 0

        mask = np.isinf(minHashClustering._precomputed_graph.data)
        minHashClustering._precomputed_graph.data[mask] = 0

    labels_clustering = minHashClustering.predict(minHashClustering._precomputed_graph, pPca=args.noPCA, pPcaDimensions=args.dimensionsPCA)

    if args.createScatterPlot:
        if args.noPCA and args.noUMAP:
            pca = PCA(n_components=min(minHashClustering._precomputed_graph.shape) - 1)
            neighborhood_matrix_knn = pca.fit_transform(minHashClustering._precomputed_graph.todense())
        else:
            neighborhood_matrix_knn = minHashClustering._precomputed_graph

        list(set(labels_clustering))

        colors = process_cmap(args.colorMap)

        try:
            neighborhood_matrix_knn = neighborhood_matrix_knn.toarray()
        except Exception:
            pass

        label_x = 'PC1'
        label_y = 'PC2'
        if not (args.noUMAP):
            label_x = 'UMAP1'
            label_y = 'UMAP2'
        if args.cell_coloring_type:
            if len(colors) < len(cell_type_color_dict):
                log.error('The chosen colormap offers too less values for the number of clusters.')
                exit(1)
            labels_clustering_cell_type = []
            for cell_name in matrices_list:
                labels_clustering_cell_type.append(cell_type_color_dict[cell_name_cell_type_dict[cell_name]])

            labels_clustering_cell_type = np.array(labels_clustering_cell_type)

            log.debug('labels_clustering_cell_type: {}'.format(len(labels_clustering_cell_type)))
            log.debug('matrices_list: {}'.format(len(matrices_list)))

            plt.figure(figsize=(args.figuresize[0], args.figuresize[1]))
            for i, color in enumerate(colors[:len(cell_type_color_dict)]):
                mask = labels_clustering_cell_type == i
                log.debug('plot cluster: {} {}'.format(color_cell_type_dict[i], np.sum(mask)))
                plt.scatter(neighborhood_matrix_knn[:, 0].T[mask], neighborhood_matrix_knn[:, 1].T[mask], color=color, label=str(color_cell_type_dict[i]), s=20, alpha=0.7)

            plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', fontsize=args.fontsize)
            plt.xticks([])
            plt.yticks([])
            plt.xlabel(label_x, fontsize=args.fontsize)
            plt.ylabel(label_y, fontsize=args.fontsize)
            if '.' not in args.createScatterPlot:
                args.createScatterPlot += '.png'
            scatter_plot_name = '.'.join(args.createScatterPlot.split('.')[:-1]) + '_cell_color.' + args.createScatterPlot.split('.')[-1]
            plt.tight_layout()
            plt.savefig(scatter_plot_name, dpi=args.dpi)
            plt.close()

            # compute overlap of cell_type find found clusters
            computed_clusters = set(labels_clustering)
            cell_type_amounts_dict = {}
            percentage_threshold = 0.8
            if args.latexTable:

                for threshold in [0.7, 0.8, 0.9]:
                    cell_type_amounts_dict[threshold] = {}
                with open(args.latexTable, 'w') as matches_file:
                    header = '\\begin{table}[!htb]\n\\footnotesize\n\\begin{tabular}{|l'
                    body = '\\hline Cluster '
                    for i in range(len(color_cell_type_dict)):
                        mask_cell_type = labels_clustering_cell_type == i
                        header += '|c'
                        body += '& ' + str(color_cell_type_dict[i]) + ' (' + str(np.sum(mask_cell_type)) + ' cells)'
                    header += '|}\n'
                    body += '\\\\\n'
                    # body = ''
                    for i in computed_clusters:
                        body += '\\hline Cluster ' + str(i)
                        mask_computed_clusters = labels_clustering == i
                        body += ' (' + str(np.sum(mask_computed_clusters)) + ' cells)'
                        for j in range(len(cell_type_color_dict)):
                            mask_cell_type = labels_clustering_cell_type == j
                            mask = mask_computed_clusters & mask_cell_type
                            number_of_matches = np.sum(mask)
                            body += '& ' + str(number_of_matches)

                            if number_of_matches != 1:
                                body += ' cells / '
                            else:
                                body += ' cell / '

                            body += '{:.2f}'.format((number_of_matches / np.sum(mask_computed_clusters)) * 100) + ' \\% '
                            for threshold in [0.7, 0.8, 0.9]:

                                if number_of_matches / np.sum(mask_computed_clusters) >= threshold:
                                    if color_cell_type_dict[j] in cell_type_amounts_dict[threshold]:
                                        cell_type_amounts_dict[threshold][color_cell_type_dict[j]] += number_of_matches
                                    else:
                                        cell_type_amounts_dict[threshold][color_cell_type_dict[j]] = number_of_matches
                                else:
                                    if color_cell_type_dict[j] in cell_type_amounts_dict[threshold]:
                                        continue
                                    else:
                                        cell_type_amounts_dict[threshold][color_cell_type_dict[j]] = 0
                        body += '\\\\\n'
                    body += '\\hline ' + '&' * len(cell_type_color_dict) + '\\\\\n'

                    for threshold in [0.7, 0.8, 0.9]:
                        body += '\\hline Correct identified $>{}\\%$'.format(int(threshold * 100))
                        for i in range(len(cell_type_color_dict)):
                            mask_cell_type = labels_clustering_cell_type == i

                            if color_cell_type_dict[i] in cell_type_amounts_dict[threshold]:
                                body += '& ' + str(cell_type_amounts_dict[threshold][color_cell_type_dict[i]]) + ' / ' + str(np.sum(mask_cell_type)) + ' ('
                                body += '{:.2f}'.format((cell_type_amounts_dict[threshold][color_cell_type_dict[i]] / np.sum(mask_cell_type)) * 100)
                            else:
                                body += '& ' + str(0) + ' / ' + str(np.sum(mask_cell_type)) + ' ('
                                body += '{:.2f}'.format(0 / np.sum(mask_cell_type))

                            body += ' \\%)'
                        body += '\\\\\n'
                    body += '\\hline \n'
                    body += '\\end{tabular}\n\\caption{}\n\\end{table}'

                    matches_file.write(header)
                    matches_file.write(body)
            else:
                with open('matches.txt', 'w') as matches_file:
                    for i in computed_clusters:
                        mask_computed_clusters = labels_clustering == i
                        for j in range(len(cell_type_color_dict)):
                            mask_cell_type = labels_clustering_cell_type == j

                            mask = mask_computed_clusters & mask_cell_type

                            number_of_matches = np.sum(mask)
                            matches_file.write('Computed cluster {} (size: {}) matching with cell type {} (size: {}) {} times. Rate (matches/computed_clusters): {}%\n'.format(
                                i, np.sum(mask_computed_clusters), color_cell_type_dict[j], np.sum(mask_cell_type), number_of_matches, number_of_matches / np.sum(mask_computed_clusters)))

                            if number_of_matches / np.sum(mask_computed_clusters) >= percentage_threshold:
                                if color_cell_type_dict[j] in cell_type_amounts_dict:
                                    cell_type_amounts_dict[color_cell_type_dict[j]] += number_of_matches
                                else:
                                    cell_type_amounts_dict[color_cell_type_dict[j]] = number_of_matches

                        matches_file.write('\n')
            all_detected = 0
            all_possible = 0
            for i in range(len(cell_type_color_dict)):

                mask_cell_type = labels_clustering_cell_type == i
                all_possible += np.sum(mask_cell_type)
                if color_cell_type_dict[i] in cell_type_amounts_dict:
                    all_detected += cell_type_amounts_dict[color_cell_type_dict[i]]
                    cell_type_amounts_dict[color_cell_type_dict[i]] /= np.sum(mask_cell_type)
                else:
                    cell_type_amounts_dict[color_cell_type_dict[i]] = 0.0
            correct_associated = 0.0
            for cell_iterator in cell_type_color_dict:
                correct_associated += cell_type_amounts_dict[cell_iterator]

            correct_associated /= len(cell_type_amounts_dict)

            # all_detected /= all_possible

            # correct_associated = ((correct_associated*4) + (all_detected)) / 5
            # correct_associated = correct_associated

            with open('correct_associated', 'w') as file:
                file.write(str(correct_associated))
        if args.cell_coloring_batch:
            if len(colors) < len(cell_type_color_dict_batch):
                log.error('The chosen colormap offers too less values for the number of clusters.')
                exit(1)
            labels_clustering_cell_type_batch = []
            for cell_name in matrices_list:
                labels_clustering_cell_type_batch.append(cell_type_color_dict_batch[cell_name_cell_type_dict_batch[cell_name]])

            labels_clustering_cell_type_batch = np.array(labels_clustering_cell_type_batch)

            log.debug('labels_clustering_cell_type: {}'.format(len(labels_clustering_cell_type_batch)))
            log.debug('matrices_list: {}'.format(len(matrices_list)))

            plt.figure(figsize=(args.figuresize[0], args.figuresize[1]))
            for i, color in enumerate(colors[:len(cell_type_color_dict_batch)]):
                mask = labels_clustering_cell_type_batch == i
                log.debug('plot cluster: {} {}'.format(color_cell_type_dict_batch[i], np.sum(mask)))
                plt.scatter(neighborhood_matrix_knn[:, 0].T[mask], neighborhood_matrix_knn[:, 1].T[mask], color=color, label=str(color_cell_type_dict_batch[i]), s=20, alpha=0.7)

            plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', fontsize=args.fontsize)
            plt.xticks([])
            plt.yticks([])
            plt.xlabel(label_x, fontsize=args.fontsize)
            plt.ylabel(label_y, fontsize=args.fontsize)
            if '.' not in args.createScatterPlot:
                args.createScatterPlot += '.png'
            scatter_plot_name = '.'.join(args.createScatterPlot.split('.')[:-1]) + '_cell_color_batch.' + args.createScatterPlot.split('.')[-1]
            plt.tight_layout()
            plt.savefig(scatter_plot_name, dpi=args.dpi)
            plt.close()

        plt.figure(figsize=(args.figuresize[0], args.figuresize[1]))
        for i, color in enumerate(colors[:args.numberOfClusters]):
            mask = labels_clustering == i
            plt.scatter(neighborhood_matrix_knn[:, 0].T[mask], neighborhood_matrix_knn[:, 1].T[mask], color=color, label=str(i), s=20, alpha=0.7)
        plt.legend(fontsize=args.fontsize)
        plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', fontsize=args.fontsize)

        plt.xticks([])
        plt.yticks([])
        plt.xlabel(label_x, fontsize=args.fontsize)
        plt.ylabel(label_y, fontsize=args.fontsize)
        if '.' not in args.createScatterPlot:
            args.createScatterPlot += '.png'
        scatter_plot_name = '.'.join(args.createScatterPlot.split('.')[:-1]) + '.' + args.createScatterPlot.split('.')[-1]
        plt.tight_layout()
        plt.savefig(scatter_plot_name, dpi=args.dpi)
        plt.close()

    matrices_cluster = list(zip(matrices_list, labels_clustering))
    np.savetxt(args.outFileName, matrices_cluster, fmt="%s")
示例#25
0
 def test_process_cmap_invalid_str(self):
     with self.assertRaises(ValueError):
         process_cmap('NonexistentColorMap', 3)
示例#26
0
 def _color(self):
     return process_cmap(self.color_map, 1)[0]
示例#27
0
 def test_mpl_colormap_name_palette(self):
     colors = process_cmap('Greys', 3)
     self.assertEqual(colors, ['#ffffff', '#959595', '#000000'])
示例#28
0
    def __init__(self,
                 data,
                 x,
                 y,
                 kind=None,
                 by=None,
                 use_index=True,
                 group_label='Variable',
                 value_label='value',
                 backlog=1000,
                 persist=False,
                 use_dask=False,
                 crs=None,
                 fields={},
                 groupby=None,
                 dynamic=True,
                 width=700,
                 height=300,
                 shared_axes=True,
                 grid=False,
                 legend=True,
                 rot=None,
                 title=None,
                 xlim=None,
                 ylim=None,
                 clim=None,
                 xticks=None,
                 yticks=None,
                 logx=False,
                 logy=False,
                 loglog=False,
                 hover=True,
                 subplots=False,
                 label=None,
                 invert=False,
                 stacked=False,
                 colorbar=None,
                 fontsize=None,
                 colormap=None,
                 datashade=False,
                 rasterize=False,
                 row=None,
                 col=None,
                 figsize=None,
                 debug=False,
                 xaxis=True,
                 yaxis=True,
                 framewise=True,
                 aggregator=None,
                 projection=None,
                 global_extent=False,
                 geo=False,
                 precompute=False,
                 flip_xaxis=False,
                 flip_yaxis=False,
                 dynspread=False,
                 hover_cols=[],
                 x_sampling=None,
                 y_sampling=None,
                 **kwds):

        # Process data and related options
        self._process_data(kind, data, x, y, by, groupby, row, col, use_dask,
                           persist, backlog, label, value_label, hover_cols,
                           kwds)
        self.use_index = use_index
        self.value_label = value_label
        self.group_label = group_label
        self.dynamic = dynamic
        self.geo = geo or crs or global_extent or projection
        self.crs = process_crs(crs) if self.geo else None
        self.row = row
        self.col = col

        # Operations
        self.datashade = datashade
        self.rasterize = rasterize
        self.dynspread = dynspread
        self.aggregator = aggregator
        self.precompute = precompute
        self.x_sampling = x_sampling
        self.y_sampling = y_sampling

        # By type
        self.subplots = subplots
        self._by_type = NdLayout if subplots else NdOverlay

        # Process options
        style_opts, plot_opts, kwds = self._process_style(colormap, kwds)
        self.stacked = stacked
        self.invert = invert
        plot_opts['logx'] = logx or loglog
        plot_opts['logy'] = logy or loglog
        plot_opts['show_grid'] = grid
        plot_opts['shared_axes'] = shared_axes
        plot_opts['show_legend'] = legend
        if xticks:
            plot_opts['xticks'] = xticks
        if yticks:
            plot_opts['yticks'] = yticks
        if not xaxis:
            plot_opts['xaxis'] = None
        if not yaxis:
            plot_opts['yaxis'] = None
        if flip_xaxis:
            plot_opts['invert_xaxis'] = True
        if flip_yaxis:
            plot_opts['invert_yaxis'] = True
        if width:
            plot_opts['width'] = width
        if height:
            plot_opts['height'] = height
        if fontsize:
            plot_opts['fontsize'] = fontsize
        if isinstance(colorbar, bool):
            plot_opts['colorbar'] = colorbar
        elif self.kind in self._colorbar_types:
            plot_opts['colorbar'] = True
        if invert:
            plot_opts['invert_axes'] = kind != 'barh'
        if rot:
            axis = 'yrotation' if invert else 'xrotation'
            plot_opts[axis] = rot
        if hover:
            plot_opts['tools'] = ['hover']

        if self.crs and global_extent:
            plot_opts['global_extent'] = global_extent
        if projection:
            plot_opts['projection'] = process_crs(projection)
        plot_opts['legend_position'] = 'right'
        if title is not None:
            plot_opts['title_format'] = title
        self._plot_opts = plot_opts
        options = Store.options(backend='bokeh')
        el_type = self._kind_mapping[self.kind].__name__
        style = options[el_type].groups['style']
        cycled_opts = [
            k for k, v in style.kwargs.items() if isinstance(v, Cycle)
        ]
        for opt in cycled_opts:
            color = style_opts.get('color', None)
            if color is None:
                color = process_cmap(colormap or 'Category10',
                                     categorical=True)
            style_opts[opt] = Cycle(
                values=color) if isinstance(color, list) else color
        self._style_opts = style_opts
        self._norm_opts = {'framewise': framewise, 'axiswise': not shared_axes}
        self.kwds = kwds

        # Process dimensions and labels
        self.label = label
        self._relabel = {'label': label} if label else {}
        self._dim_ranges = {
            'x': xlim or (None, None),
            'y': ylim or (None, None),
            'c': clim or (None, None)
        }
        self._redim = fields

        # High-level options
        self._validate_kwds(kwds)
        if debug:
            kwds = dict(x=self.x,
                        y=self.y,
                        by=self.by,
                        kind=self.kind,
                        groupby=self.groupby)
            self.warning('Plotting {kind} plot with parameters x: {x}, '
                         'y: {y}, by: {by}, groupby: {groupby}'.format(**kwds))
示例#29
0
"""
This file defines palettes used for EDA.
"""
# pylint: disable=no-name-in-module
from bokeh.palettes import Category20  # type: ignore
from holoviews.plotting.util import process_cmap

PALETTE = Category20[20]
BIPALETTE = list(reversed(process_cmap("RdBu")))
BRG = ["#1f78b4", "#d62728", "#2ca02c"]
示例#30
0
    def __call__(self, kind, x, y):
        kind = self.kind or kind
        method = getattr(self, kind)

        groups = self.groupby
        zs = self.kwds.get('z', [])
        if not isinstance(zs, list): zs = [zs]
        grid = []
        if self.row: grid.append(self.row)
        if self.col: grid.append(self.col)
        groups += grid
        if groups or len(zs) > 1:
            if self.streaming:
                raise NotImplementedError(
                    "Streaming and groupby not yet implemented")
            data = self.data
            if not self.gridded and any(g in self.indexes for g in groups):
                data = data.reset_index()
            dataset = Dataset(data)
            if groups:
                dataset = dataset.groupby(groups, dynamic=self.dynamic)
                if len(zs) > 1:
                    dimensions = [Dimension(self.group_label, values=zs)
                                  ] + dataset.kdims
                    if self.dynamic:
                        obj = DynamicMap(
                            lambda *args: getattr(self, kind)
                            (x, y, args[0], dataset[args[1:]].data),
                            kdims=dimensions)
                    else:
                        obj = HoloMap(
                            {(z, ) + k: getattr(self, kind)(x, y, z,
                                                            dataset[k])
                             for k, v in dataset.data.items() for z in zs},
                            kdims=dimensions)
                else:
                    obj = dataset.map(
                        lambda ds: getattr(self, kind)(x, y, data=ds.data),
                        Dataset)
            elif len(zs) > 1:
                if self.dynamic:
                    dataset = DynamicMap(
                        lambda z: getattr(self, kind)
                        (x, y, z, data=dataset.data),
                        kdims=[Dimension(self.group_label, values=zs)])
                else:
                    dataset = HoloMap(
                        {
                            z: getattr(self, kind)(x, y, z, data=dataset.data)
                            for z in zs
                        },
                        kdims=[self.group_label])
            else:
                obj = getattr(self, kind)(x, y, data=dataset.data)
            if grid:
                obj = obj.grid(grid).options(shared_xaxis=True,
                                             shared_yaxis=True)
        else:
            if self.streaming:
                cbcallable = StreamingCallable(partial(method, x, y),
                                               periodic=self.cb)
                obj = DynamicMap(cbcallable, streams=[self.stream])
            else:
                obj = method(x, y)

        if not (self.datashade or self.rasterize):
            return obj

        try:
            from holoviews.operation.datashader import datashade, rasterize, dynspread
            from datashader import count_cat
        except:
            raise ImportError('Datashading is not available')

        opts = dict(width=self._plot_opts['width'],
                    height=self._plot_opts['height'],
                    dynamic=self.dynamic)
        if 'cmap' in self._style_opts and self.datashade:
            levels = self._plot_opts.get('color_levels')
            opts['cmap'] = process_cmap(self._style_opts['cmap'], levels)
            opts['color_key'] = opts['cmap']
        if self.by:
            opts['aggregator'] = count_cat(self.by[0])
        if self.aggregator:
            opts['aggregator'] = self.aggregator
        if self.precompute:
            opts['precompute'] = self.precompute
        if self.x_sampling:
            opts['x_sampling'] = self.x_sampling
        if self.y_sampling:
            opts['y_sampling'] = self.y_sampling
        style = {}
        if self.datashade:
            operation = datashade
            eltype = 'RGB'
        else:
            operation = rasterize
            eltype = 'Image'
            if 'cmap' in self._style_opts:
                style['cmap'] = self._style_opts['cmap']

        if self.crs:
            # Apply projection before rasterizing
            import cartopy.crs as ccrs
            from geoviews import project
            projection = self._plot_opts.get('projection',
                                             ccrs.GOOGLE_MERCATOR)
            obj = project(obj, projection=projection)

        processed = operation(obj, **opts)

        if self.dynspread:
            if self.datashade:
                processed = dynspread(processed,
                                      max_px=self.kwds.get('max_px', 3),
                                      threshold=self.kwds.get(
                                          'threshold', 0.5))
            else:
                self.warning(
                    'dynspread may only be applied on datashaded plots, '
                    'use datashade=True instead of rasterize=True.')
        return processed.opts(
            {eltype: {
                'plot': self._plot_opts,
                'style': style
            }})
示例#31
0
get_background_pnCCD = True  # Background for plottting: This uses an averaged background - mostly for plotting
get_background_max_pnCCD = True  # Background for Hitfinder: This is the background for hitfinder only it should contain on each pixel the max value from background run

# DISPLAY SETTINGS
## set display frequency relative to analysis frequency --> display routine (update displayed plots) every xxth analyzed shot
### this is neccessary because you should do the next display update only when the last update completed, otherwise update can pile up, leading to slowdown and lag of display / tool
### typically displaying takes 5 to 7 analyzed shots which is why 9 or 10 might be a good value
oa_disp_mod = 9
## setting changes size of last 5 pnccd detected hits plots
large_monitor = True
## here you can select a colormap that will be used for all image plots
### you can find a selection of available colormaps online: holoviews.org/user_guide/Colormaps.html
### examples:
### colormap_pnCCD = 'Plasma'
### colormap_pnCCD = process_cmap('rainbow',provider='colorcet')
colormap_pnCCD = process_cmap('jet',
                              provider='matplotlib')  # by default we use jet

######### PARAMETERS CALCULATED FROM USER PARAMETERS #########
# Initialize PNCCD Backgrounds
background_pnCCD = np.zeros(shape=(1024, 1024))
background_max_pnCCD = np.zeros(shape=(1024, 1024))
# Load Files into background arrays if background substraction was actived
if get_background_pnCCD:
    background_pnCCD = np.load('background_pnCCD.npy')
if get_background_max_pnCCD:
    background_max_pnCCD = np.load('background_max_pnCCD.npy')
# TOF Trace Config Values
end_tof = start_tof + N_datapts  # index of last TOF datapoint considered
x_tof = np.arange(start_tof, end_tof)  # x-axis for tof data points
# PNCCD Dimensions
pnccd_dims = [
示例#32
0
# styling options
opts = {
    'Image':
    dict(height=80,
         width=280,
         xaxis=None,
         yaxis=None,
         toolbar=None,
         border=14,
         show_frame=False)
}

# create a plot of 4 uniform sequential colormaps
cms = filter_cmaps('Uniform Sequential')
cms = list(cms[i] for i in [0, 6, 12, 20])  # select 4 specific colormaps

hv_cmap = [hv.Image(spacing, ydensity=1, label="{0}".format(r.name)).options(cmap=process_cmap(r.name))\
            .options(opts) for r in cms]
cmap_seq_viz = (hv_cmap[0] + hv_cmap[1] + hv_cmap[2] +
                hv_cmap[3]).cols(2).options(toolbar=None)

# create a plot of 4 diverging colormaps
cms = filter_cmaps('Diverging')
cms = list(cms[i] for i in [4, 8, 14, 23])

hv_cmap = [hv.Image(spacing, ydensity=1, label="{0}".format(r.name)).options(cmap=process_cmap(r.name))\
            .options(opts) for r in cms]
cmap_div_viz = (hv_cmap[0] + hv_cmap[1] + hv_cmap[2] +
                hv_cmap[3]).cols(2).options(toolbar=None)