Exemplo n.º 1
0
    def plot_3D(self, type, fname):
        x = self.data['XWIN']
        y = self.data['YWIN']
        oceta = self.data['O-Ceta']
        oxi = self.data['O-Cxi']
        resids = [(((oceta[i]**2) + (oxi[i]**2))**.5)
                  for i in range(len(oceta))]
        if type == 'trisurf':
            z = resids
            fig = plt.figure()
            ax = fig.gca(projection='3d')
            ax.set_xlabel('XWIN')
            ax.set_ylabel('YWIN')
            ax.set_zlabel('magnitude of residual')
            ax.plot_trisurf(x, y, z, cmap='magma')
            plt.savefig(os.path.join(self.directory, fname + '.png'))
            plt.show()

        elif type == 'quiver':
            x = np.array(x)
            y = np.array(y)
            over = self.searchhighestresids(90)
            #x = [self.data['XWIN'][i] for i in over]
            #y = [self.data['YWIN'][i] for i in over]
            z = np.array(resids)
            u = np.array(self.data['O-Ceta'])
            v = np.array(self.data['O-Cxi'])
            w = np.array([0 for i in range(len(v))])
            p3.clear()
            quiver = p3.quiver(x, y, z, u, v, w, size=3)
            #p3.savefig(os.path.join(self.directory, fname + '.png'))
            p3.show()
Exemplo n.º 2
0
def plot_mesh(value_mesh_list, w1_mesh, w2_mesh, save, show_box, show_axes):
    ipv.clear()
    figs = []
    for mesh in value_mesh_list:
        fig = ipv.pylab.figure()
        fig.camera.up = (0, 0, 1)
        fig.camera.position = (-2, 1, -0.5)
        fig.camera_control = "trackball"
        if not show_axes:
            ipv.style.box_off()
        else:
            ipv.xlabel("w1")
            ipv.ylabel("w2")
            ipv.zlabel("f_lambda")
        if not show_box:
            ipv.pylab.style.axes_off()

        ipv.pylab.zlim(mesh.min(), mesh.max())
        ptp = (mesh - mesh.min()).ptp()

        col = []
        for m in mesh:
            znorm = (m - m.min()) / (m.max() - m.min() + 1e-8)
            color = np.asarray([[interpolate(darker(colors_alpha[0], 1.5 * x + 0.75),
                                             darker(colors_alpha[1], 1.5 * (1 - x) + 0.75), x) for x in y] for y in
                                znorm])
            col.append(color)
        color = np.array(col)
        surf = ipv.plot_surface(w1_mesh, w2_mesh, mesh, color=color[..., :3])
        ipv.animation_control(surf, interval=400)
        figs.append(fig)
        ipv.show()
    if save:
        ipv.save(f'renders/{datetime.datetime.now().strftime("%m%d-%H%M")}.html', offline=True)
    return figs
Exemplo n.º 3
0
def browse_history(history,
                   coords=["x", "y", "z"],
                   start=None,
                   stop=None,
                   size=None,
                   **draw_specs_kw):
    times = history.slice(start, stop, size)
    num_frames = times.size
    draw_specs = sheet_spec()
    spec_updater(draw_specs, draw_specs_kw)
    sheet = history.retrieve(0)
    ipv.clear()
    fig, meshes = sheet_view(sheet, coords, **draw_specs_kw)

    lim_inf = sheet.vert_df[sheet.coords].min().min()
    lim_sup = sheet.vert_df[sheet.coords].max().max()
    ipv.xyzlim(lim_inf, lim_sup)

    def set_frame(i=0):
        fig.animation = 0
        t = times[i]
        meshes = _get_meshes(history.retrieve(t), coords, draw_specs)
        update_view(fig, meshes)

    ipv.show()
    interact(set_frame, i=(0, num_frames - 1))
Exemplo n.º 4
0
def complete(final_solution: Solution,
             correct_solution: Solution = None) -> None:
    """
    Will print result in 3D world
    :return: Noting
    """

    # init of plot-output #1
    figure = plot.figure()
    axes = figure.add_subplot(111, projection='3d')

    for part in final_solution.partitions:
        color = random.choice(list_of_colors)
        marker = random.choice(list_of_marker)
        for p in part:
            axes.scatter(p.x, p.y, p.z, marker=marker, c=color)

    axes.set_xlabel("X Achse")
    axes.set_ylabel("Y Achse")
    axes.set_zlabel("Z Achse")
    plot.show()

    # init 3D view
    ipv.current.containers.clear()
    ipv.current.figures.clear()

    extra_figures = list()
    # create correct solution
    if correct_solution is not None:
        figure_correct = ipv.figure("correct")
        ipv.pylab.xyzlim(-1, 11)
        for part in correct_solution.partitions:
            marker = random.choice(list_of_IPYVmarker)
            color = random.choice(list_of_colors)
            ipv.scatter(*convert.partition_to_IpyVolume(part),
                        marker=marker,
                        color=color)
        extra_figures.append(figure_correct)
    figure_result = ipv.figure("result")
    container = ipv.gcc()
    ipv.current.container = widgets.HBox(container.children)
    ipv.current.containers["result"] = ipv.current.container

    # crate computed solution
    for part in final_solution.partitions:
        marker = random.choice(list_of_IPYVmarker)
        color = random.choice(list_of_colors)
        ipv.scatter(*convert.partition_to_IpyVolume(part),
                    marker=marker,
                    color=color)

    ipv.pylab.xyzlim(-1, 11)
    ipv.current.container.children = list(
        ipv.current.container.children) + extra_figures
    ipv.show()
    # save in a separate file
    ipv.save("example.html")

    # destroy 3D views
    ipv.clear()
Exemplo n.º 5
0
    def plot_clusters(self, threshold, rcut=28, size=5.):
        ipv.clear()

        s = self.trace <= threshold
        xyz = self.stress_coord[s]

        x, y, z = xyz[:, 0], xyz[:, 1], xyz[:, 2]
        clustering = DBSCAN(eps=rcut, min_samples=1).fit(xyz)
        labels = np.unique(clustering.labels_)

        counts, edges = np.histogram(clustering.labels_,
                                     bins=np.arange(labels.min(),
                                                    labels.max() + 1))

        largest = edges[counts.argmax()]

        color_dict = {}
        for l in labels:
            color_dict[l] = tuple(np.random.uniform(0, 1, size=3))

        color_dict[largest] = (1, 1, 1)
        colors = np.array([color_dict[c] for c in clustering.labels_])

        color = np.array([(x - x.min()) / x.ptp(),
                          np.ones_like(x),
                          np.ones_like(x)]).T
        ipv.scatter(x, y, z, size=size, marker="sphere", color=colors)

        ipv.show()
Exemplo n.º 6
0
def scatter_nd(xs: t.Tensor):
    xs = full_detach(xs)

    if xs.shape[1] == 1:
        plt.hist(xs[:, 0], bins=100)
    elif xs.shape[1] == 2:
        plt.scatter(*xs.T, s=.1)
    elif xs.shape[1] == 3:
        ipv.clear()
        ipv.scatter(*xs.T)
        ipv.show()
    else:
        assert False, f"Wrong dimensionality: {xs.shape=}"
Exemplo n.º 7
0
    def __init__(self, eptm, **draw_specs):

        plt.ioff()
        ipv.clear()
        self.fig3D, self.mesh = sheet_view(eptm, mode="3D", **draw_specs)
        self.graph_widget = widgets.Output()
        with self.graph_widget:

            self.fig2D, (self.ax0, self.ax1) = plt.subplots(
                2, 1, sharey=True, sharex=True
            )

            apical = eptm.get_sub_sheet("apical")
            apical.reset_index()
            apical.reset_topo()
            apical.face_df["visible"] = apical.face_df["y"] > 0
            _ = sheet_view(
                apical,
                mode="2D",
                coords=["z", "x"],
                ax=self.ax0,
                edge={"visible": False},
                face={"visible": True, "color": apical.face_df.area},
            )
            basal = eptm.get_sub_sheet("basal")
            basal.reset_index()
            basal.reset_topo()
            basal.face_df["visible"] = basal.face_df["y"] > 0
            _ = sheet_view(
                basal,
                mode="2D",
                coords=["z", "x"],
                ax=self.ax1,
                edge={"visible": False},
                face={"visible": True, "color": basal.face_df.area},
            )
            self.ax0.set_title("Apical mesh")
            self.ax1.set_title("Basal mesh")
            self.ax0.set_axis_off()
            self.ax1.set_axis_off()
            self.fig2D.set_size_inches(5, 8)
            plt.close(self.fig2D)
            display(self.fig2D)

        super().__init__([self.fig3D, self.graph_widget])
Exemplo n.º 8
0
def test_sheet_view():

    sheet = Sheet("test", *three_faces_sheet())
    SheetGeometry.update_all(sheet)
    face_spec = {
        "color": pd.Series(range(3)),
        "color_range": (0, 3),
        "visible": True,
        "colormap": "Blues",
        "epsilon": 0.1,
    }

    color = pd.DataFrame(np.zeros((sheet.Ne, 3)),
                         index=sheet.edge_df.index,
                         columns=["R", "G", "B"])

    color.loc[0, "R"] = 0.8

    edge_spec = {"color": color, "visible": True}
    fig, (edge_mesh, face_mesh) = sheet_view(sheet,
                                             face=face_spec,
                                             edge=edge_spec)
    assert face_mesh.color.shape == (39, 3)
    assert face_mesh.triangles.shape == (18, 3)
    assert face_mesh.lines is None
    assert edge_mesh.triangles is None
    assert edge_mesh.lines.shape == (18, 2)
    sheet.face_df["visible"] = False
    sheet.face_df.loc[0, "visible"] = True
    ipv.clear()
    fig, (edge_mesh, face_mesh) = sheet_view(sheet,
                                             face=face_spec,
                                             edge=edge_spec)
    assert face_mesh.triangles.shape == (6, 3)

    ipv.clear()
    edge_spec = {"color": lambda sheet: sheet.edge_df["dx"], "visible": True}
    face_spec = {"color": lambda sheet: sheet.face_df["area"], "visible": True}
    fig, (edge_mesh, face_mesh) = sheet_view(sheet,
                                             face=face_spec,
                                             edge=edge_spec)
    assert face_mesh.color.shape == (13, 3)
def show_attention_volume(
    scan,
    attention_maps,
    tasks=[],
    show_ct=True,
    show_pet=False,
    downscale=1,
    exam_id="",
    movie_dir=None,
    colormap=None,
    flip_axes=None,
):

    colors = plt.get_cmap("Set1").colors
    widgets = []

    def add_controls(volume, name, color=None):
        """
        """
        level = FloatLogSlider(
            base=10, min=-0.5, max=0, step=0.01, description=f"{name} level:"
        )
        opacity = FloatLogSlider(
            base=10, min=-2, max=0.6, step=0.01, description=f"{name} opacity:"
        )
        jslink((volume.tf, "level1"), (level, "value"))
        jslink((volume.tf, "level2"), (level, "value"))
        jslink((volume.tf, "level3"), (level, "value"))
        jslink((volume, "opacity_scale"), (opacity, "value"))

        button = Button(description=name)
        if color is not None:
            button.style.button_color = color
        controls = HBox([button, level, opacity])
        widgets.append(controls)

    ipv.clear()
    f = ipv.figure()

    for idx, task in enumerate(tasks):
        attention_map = attention_maps[task]
        attention_map = attention_map.cpu().detach().numpy()
        attention_map = np.mean(attention_map, axis=1)
        attention_map = attention_map[0, :, ::].squeeze()

        # scale volume up
        scan_shape = scan[:, :, :, :, 0].squeeze().shape
        for dim_idx, scan_dim in enumerate(scan_shape):
            repeat = np.round(scan_dim / attention_map.shape[dim_idx])
            attention_map = np.repeat(attention_map, repeat, axis=dim_idx)

        # set color
        if colormap is None:
            color = np.array(colors[idx % len(colors)])
        else:
            color = np.array(colormap[task]) / 256
        opacity = color * 0.2
        color = "#%02x%02x%02x" % tuple(map(int, color * 255))

        attention_map = attention_map[::downscale, ::downscale, ::downscale]
        if flip_axes is not None:
            for flip_axis in flip_axes:
                attention_map = np.flip(attention_map, axis=flip_axis)
        saliency_vol = ipv.volshow(
            attention_map,
            level=(1.0, 1.0, 1.0),
            opacity=opacity,
            controls=True,
            extent=[
                [0, attention_map.shape[0]],
                [0, attention_map.shape[1]],
                [0, attention_map.shape[2]],
            ],
        )

        add_controls(saliency_vol, name=task, color=color)

    if show_ct:
        ct = scan[:, :, :, :, 0].squeeze()
        ct = ct[::downscale, ::downscale, ::downscale]
        if flip_axes is not None:
            for flip_axis in flip_axes:
                ct = np.flip(ct, axis=flip_axis)
        ct_vol = ipv.volshow(
            ct,
            downscale=100,
            level=(0.7, 0.7, 0.7),
            opacity=(0.2, 0.2, 0.2),
            extent=[[0, ct.shape[0]], [0, ct.shape[1]], [0, ct.shape[2]]],
        )
        add_controls(ct_vol, name="ct")
    if show_pet:
        pet = scan[:, :, :, :, 1].squeeze()
        pet = pet[::downscale, ::downscale, ::downscale]
        if flip_axes is not None:
            for flip_axis in flip_axes:
                pet = np.flip(pet, axis=flip_axis)
        color = np.array(colors[0 % len(colors)])
        opacity = color * 0.2
        pet_vol = ipv.volshow(
            pet, downscale=100, level=(0.7, 0.7, 0.7), opacity=opacity
        )
        add_controls(pet_vol, name="pet")

    # ipv.xlim(0,attention_map.shape[0])
    # ipv.ylim(0,attention_map.shape[1])
    # ipv.zlim(0,attention_map.shape[2])
    # ipv.squarelim()

    # f.camera.up = (-1, -1, -1)
    # f.camera.lookAt = (0, 0, 0)
    ipv.pylab.view(0, 0, 0)
    if movie_dir is not None:
        ipv.pylab.movie(f=os.path.join(movie_dir, f"{exam_id}.gif"))

    ipv.style.use("minimal")
    widgets.append(ipv.gcf())
    return VBox(widgets)
Exemplo n.º 10
0
    def _default_plotter(self, **kwargs):
        """
        Basic plot function to be used if no custom function is specified.

        This is called by plot, you shouldn't call it directly.
        """

        self.lengths = np.array([length(x) for x in self.lines2use_])
        if not ("grayscale" in kwargs and kwargs["grayscale"]):
            self.colors = np.array([color(x) for x in self.lines2use_])
        else:
            self.colors = np.zeros((len(self.lines2use_), 3), dtype=np.float16)
            self.colors[:] = [0.5, 0.5, 0.5]
        self.state = {"threshold": 0, "indices": []}

        width = 600
        height = 600
        perc = 80
        if "width" in kwargs:
            width = kwargs["width"]
        if "height" in kwargs:
            height = kwargs["height"]
        if "percentile" in kwargs:
            perc = kwargs["percentile"]

        ipv.clear()
        fig = ipv.figure(width=width, height=height)
        self.state["fig"] = fig

        with fig.hold_sync():
            x, y, z, indices, colors, self.line_pointers = self._create_mesh()
            limits = np.array(
                [
                    min([x.min(), y.min(), z.min()]),
                    max([x.max(), y.max(), z.max()]),
                ]
            )
            mesh = ipv.Mesh(x=x, y=y, z=z, lines=indices, color=colors)
            fig.meshes = [mesh]
            if "style" not in kwargs:
                fig.style = {
                    "axes": {
                        "color": "black",
                        "label": {"color": "black"},
                        "ticklabel": {"color": "black"},
                        "visible": False,
                    },
                    "background-color": "white",
                    "box": {"visible": False},
                }
            else:
                fig.style = kwargs["style"]
            ipv.pylab._grow_limits(limits, limits, limits)
            fig.camera_fov = 1
        ipv.show()

        interact(
            self._plot_lines,
            state=fixed(self.state),
            threshold=widgets.FloatSlider(
                value=np.percentile(self.lengths, perc),
                min=self.lengths.min() - 1,
                max=self.lengths.max() - 1,
                continuous_update=False,
            ),
        )
Exemplo n.º 11
0
    def visualize_objects(self,
                          train_idxs=None,
                          test_idxs=None,
                          max_time_steps=None,
                          train_sim=False,
                          test_sim=True,
                          train_marker_size=4,
                          test_marker_size=6):
        """
            train_idxs - numpy array из индексов объектов (sat_id) тренировочной выборки,
                         которые надо визуализировать. Если  None - берем все объекты
                         
            test_idxs - numpy array из индексов объектов (sat_id) тренировочной выборки,
                         которые надо визуализировать. Если None - берем train_idxs
                         
            max_time_steps - максимальное количество измерений для одного объекта (sat_id)
            
            train_sim - если False - используем реальные данные (колонки без приставки sim)
            
            test_sim - если False - используем реальные (предсказанные) данные 
            (для этого в датафрейм нужно добавить приставки колонки с предсказаниями без приставки sim,
             как в трейне)
            
        """

        ipv.clear()
        if train_idxs is None:
            train_idxs = np.array(self.train_data['sat_id'].unique())
        if test_idxs is None:
            test_idxs = train_idxs

        if max_time_steps is None:
            max_time_steps_train = self.train_data.groupby(
                'sat_id').count()['epoch'].max()
            max_time_steps_test = self.test_data.groupby(
                'sat_id').count()['epoch'].max()
            max_time_steps = max(max_time_steps_train, max_time_steps_test)

        ## подготовка трейна и теста
        stream_train = self._prepare_stream('train', train_idxs,
                                            max_time_steps, train_sim)
        stream_test = self._prepare_stream('test', test_idxs, max_time_steps,
                                           test_sim)

        ## визуализация
        stream = np.dstack([stream_train[:, :, :], stream_test[:, :, :]])
        selected = stream_train.shape[2] + test_idxs
        self.q = ipv.quiver(*stream[:, :, :],
                            color="green",
                            color_selected='red',
                            size=train_marker_size,
                            size_selected=test_marker_size,
                            selected=selected)

        ##  Чтобы можно было менять размеры и цвета
        size = FloatSlider(min=1, max=15, step=0.2)
        size_selected = FloatSlider(min=1, max=15, step=0.2)
        color = ColorPicker()
        color_selected = ColorPicker()
        jslink((self.q, 'size'), (size, 'value'))
        jslink((self.q, 'size_selected'), (size_selected, 'value'))
        jslink((self.q, 'color'), (color, 'value'))
        jslink((self.q, 'color_selected'), (color_selected, 'value'))
        #         ipv.style.use('seaborn-darkgrid')
        ipv.animation_control(self.q, interval=75)
        ipv.show(
            [VBox([ipv.gcc(), size, size_selected, color, color_selected])])
Exemplo n.º 12
0
def ipv_prepare(ipv, dark_background=True):
    ipv.clear()
    if dark_background is True:
        ipv.style.set_style_dark()
Exemplo n.º 13
0
    def _default_plotter(self, **kwargs):
        """
        Basic plot function to be used if no custom function is specified.

        This is called by plot, you shouldn't call it directly.
        """

        self.lengths = np.array([length(x) for x in self.lines2use_])
        if not ('grayscale' in kwargs and kwargs['grayscale']):
            self.colors = np.array([color(x) for x in self.lines2use_])
        else:
            self.colors = np.zeros((len(self.lines2use_), 3), dtype=np.float16)
            self.colors[:] = [0.5, 0.5, 0.5]
        self.state = {'threshold': 0, 'indices': []}

        width = 600
        height = 600
        perc = 80
        if 'width' in kwargs:
            width = kwargs['width']
        if 'height' in kwargs:
            height = kwargs['height']
        if 'percentile' in kwargs:
            perc = kwargs['percentile']

        ipv.clear()
        fig = ipv.figure(width=width, height=height)
        self.state['fig'] = fig

        with fig.hold_sync():
            x, y, z, indices, colors, self.line_pointers = self._create_mesh()
            limits = np.array([
                min([x.min(), y.min(), z.min()]),
                max([x.max(), y.max(), z.max()])
            ])
            mesh = ipv.Mesh(x=x, y=y, z=z, lines=indices, color=colors)
            fig.meshes = [mesh]
            if 'style' not in kwargs:
                fig.style = {
                    'axes': {
                        'color': 'black',
                        'label': {
                            'color': 'black'
                        },
                        'ticklabel': {
                            'color': 'black'
                        },
                        'visible': False
                    },
                    'background-color': 'white',
                    'box': {
                        'visible': False
                    }
                }
            else:
                fig.style = kwargs['style']
            ipv.pylab._grow_limits(limits, limits, limits)
            fig.camera_fov = 1
        ipv.show()

        interact(self._plot_lines,
                 state=fixed(self.state),
                 threshold=widgets.FloatSlider(value=np.percentile(
                     self.lengths, perc),
                                               min=self.lengths.min() - 1,
                                               max=self.lengths.max() - 1,
                                               continuous_update=False))
Exemplo n.º 14
0
def clear():
    """Call ipyvolume.clear()."""
    ipv.clear()
def plot_multiple_proofread_neuron(
    segment_ids,
    split_indexes = None,
    cell_type = None,
    original_mesh = None,
    plot_proofread_skeleton = False, 
    
    proofread_mesh_color = "green",
    proofread_mesh_alpha = None,
    proofread_skeleton_color = "black",

    plot_nucleus = True,
    nucleus_size = 1,
    nucleus_color = "proofread_mesh_color",


    plot_synapses = True,
    synapses_size = 0.05,
    synapse_plot_type = "spine_bouton",#"compartment"#  "valid_error"
    synapse_compartments = None,
    synapse_spine_bouton_labels = None,
    plot_error_synapses = False,
    valid_synapses_color = "orange",
    error_synapses_color = "aliceblue",
    synapse_queries = None,
    synapse_queries_colors = None,

    plot_error_mesh = False,
    error_mesh_color = "black",
    error_mesh_alpha = 1,


    compartments = None,
    #compartments = ["apical_total"]
    #compartments= ["axon","dendrite"]
    plot_compartment_meshes = True,
    compartment_mesh_alpha = 0.3,
    plot_compartment_skeletons = True,
    
    
    # for adding new scatter points:
    scatters = None,
    scatter_size = 0.2,
    scatters_colors = "yellow",

    verbose = False,
    print_spine_colors = True,
    print_compartment_colors = True,
    ):
    
    
    import ipyvolume as ipv
    ipv.clear()
    
    su.ignore_warnings()
    
    segment_ids = nu.convert_to_array_like(segment_ids)
    
    if verbose:
        print(f"segment_ids = {segment_ids}")
    proofread_mesh_color = nu.convert_to_array_like(proofread_mesh_color)
    
    if len(proofread_mesh_color) != len(segment_ids):
        proofread_mesh_color = proofread_mesh_color*len(segment_ids)
    if split_indexes is None:
        split_indexes = [0]*len(segment_ids)
        
    for j,(seg_id,sp_idx,proof_col)  in enumerate(zip(segment_ids,
                                        split_indexes,
                                        proofread_mesh_color,
                                       )):

        if verbose:
            print(f"\n{seg_id}_{sp_idx}: {proof_col}")
            
        curr_scatters = None
        curr_scatter_size = None
        curr_scatters_colors = None
            
        if j == len(segment_ids)-1:
            show_at_end = True
            append_figure = True
            print_spine_colors_curr = print_spine_colors
            print_compartment_colors_curr = print_compartment_colors
            
            curr_scatters = scatters
            curr_scatter_size = scatter_size
            curr_scatters_colors = scatters_colors 
            
        elif j == 0:
            show_at_end = False
            append_figure = False
            print_spine_colors_curr = False
            print_compartment_colors_curr = False
        else:
            show_at_end = False
            append_figure = True
            print_spine_colors_curr = False
            print_compartment_colors_curr = False
            
        #print(f"{curr_scatters,curr_scatter_size,curr_scatters_colors}")
             
             
        pv.plot_proofread_neuron(
            seg_id,
            sp_idx,
            cell_type = cell_type,
            original_mesh = original_mesh,
            plot_proofread_skeleton=plot_proofread_skeleton,

            proofread_mesh_color = proof_col,
            proofread_mesh_alpha = proofread_mesh_alpha,
            proofread_skeleton_color = proofread_skeleton_color,

            plot_nucleus = plot_nucleus,
            nucleus_size = nucleus_size,
            nucleus_color = nucleus_color,


            plot_synapses = plot_synapses,
            synapses_size = synapses_size,
            synapse_plot_type = synapse_plot_type,#"compartment"#  "valid_error"
            synapse_compartments = synapse_compartments,
            synapse_spine_bouton_labels = synapse_spine_bouton_labels,
            plot_error_synapses = plot_error_synapses,
            valid_synapses_color = valid_synapses_color,
            error_synapses_color = error_synapses_color,
            synapse_queries = synapse_queries,
            synapse_queries_colors = synapse_queries_colors,

            plot_error_mesh = plot_error_mesh,
            error_mesh_color = error_mesh_color,
            error_mesh_alpha = error_mesh_alpha,


            compartments = compartments,
            #compartments = ["apical_total"]
            #compartments= ["axon","dendrite"]
            plot_compartment_meshes = plot_compartment_meshes,
            compartment_mesh_alpha = compartment_mesh_alpha,
            plot_compartment_skeletons = plot_compartment_skeletons,

            verbose = verbose,
            print_spine_colors = print_spine_colors_curr,
            print_compartment_colors = print_compartment_colors_curr,
            
            
            show_at_end = show_at_end,
            append_figure = append_figure,
            
            scatters = curr_scatters,
            scatter_sizes = curr_scatter_size,
            scatters_colors = curr_scatters_colors,

            )
def show_saliency_volume(
    saliency_maps, tasks=[], show_ct=True, show_pet=False, colormap=None
):

    colors = plt.get_cmap("Set1").colors
    widgets = []

    def add_controls(volume, name, color=None):
        """
        """
        level = FloatLogSlider(
            base=10, min=-0.5, max=0, step=0.0002, description=f"{name} level:"
        )
        opacity = FloatLogSlider(
            base=10, min=-2, max=1.0, step=0.01, description=f"{name} opacity:"
        )
        jslink((volume.tf, "level1"), (level, "value"))
        jslink((volume.tf, "level2"), (level, "value"))
        jslink((volume.tf, "level3"), (level, "value"))
        jslink((volume, "opacity_scale"), (opacity, "value"))

        button = Button(description=name)
        if color is not None:
            button.style.button_color = color
        controls = HBox([button, level, opacity])
        widgets.append(controls)

    ipv.clear()
    ipv.figure()

    data = {}

    for idx, task in enumerate(tasks):
        saliency_map = saliency_maps[task]
        scan_grad = saliency_map["scan_grad"]
        scan_grad = torch.max(scan_grad, dim=4)[0].unsqueeze(0)

        scan_grad = (
            scan_grad.squeeze()
        )  # torch.nn.functional.max_pool3d(scan_grad, kernel_size=(7, 21, 21)).squeeze()

        scan_grad = scan_grad.numpy()

        scan_grad = scan_grad - scan_grad.min()
        scan_grad /= scan_grad.max()
        saliency_tensor = scan_grad
        saliency_tensor -= saliency_tensor.min()
        saliency_tensor /= saliency_tensor.max()
        if colormap is None:
            color = np.array(colors[idx % len(colors)])
        else:
            color = np.array(colormap[task]) / 256
        opacity = color * 0.2
        color = "#%02x%02x%02x" % tuple(map(int, color * 256))
        saliency_vol = ipv.volshow(
            saliency_tensor,
            downscale=100,
            level=(0.5, 0.5, 0.5),
            opacity=opacity,
            controls=True,
        )
        data[task] = saliency_tensor

        add_controls(saliency_vol, name=task, color=color)

    scan = saliency_map["scan"].numpy()
    if show_ct:
        ct = scan[:, :, :, :, 0].squeeze()
        data["ct"] = ct
        ct_vol = ipv.volshow(
            ct, downscale=100, level=(1.0, 1.0, 1.0), opacity=(0.2, 0.2, 0.2)
        )
        add_controls(ct_vol, name="ct")
    if show_pet:
        pet = scan[:, :, :, :, 1].squeeze()
        data["pet"] = pet
        opacity = (np.array((228, 26, 28)) / 256) * 0.2
        pet_vol = ipv.volshow(
            pet, downscale=100, level=(0.7, 0.7, 0.7), opacity=opacity
        )
        add_controls(pet_vol, name="pet")

    ipv.style.use("minimal")
    widgets.append(ipv.gcf())
    return VBox(widgets), data
def plot_proofread_neuron(
    segment_id,
    split_index = 0,
    cell_type = None,
    original_mesh = None,
    
    plot_proofread_skeleton = False,
    
    proofread_mesh_color = "green",
    proofread_mesh_alpha = None,
    proofread_skeleton_color = "black",

    plot_nucleus = True,
    nucleus_size = 1,
    nucleus_color = "proofread_mesh_color",#"black",


    plot_synapses = True,
    synapses_size = 0.05,
    synapse_plot_type = "spine_bouton",#"compartment"#  "valid_error" #"valid_presyn_postsyn"
    synapse_compartments = None,
    synapse_spine_bouton_labels = None,
    plot_error_synapses = False,
    valid_synapses_color = "orange",
    error_synapses_color = "aliceblue",
    synapse_queries = None,
    synapse_queries_colors = None,

    plot_error_mesh = False,
    error_mesh_color = "black",
    error_mesh_alpha = 1,


    compartments = None,
    #compartments = ["apical_total"]
    #compartments= ["axon","dendrite"]
    plot_compartment_meshes = True,
    compartment_mesh_alpha = 0.3,
    plot_compartment_skeletons = True,

    verbose = False,
    print_spine_colors = True,
    print_compartment_colors = True,
    
    #arguments for plotting more scatters
    scatters = None,
    scatter_sizes = 0.2,
    scatters_colors = "yellow",
    
    show_at_end = True,
    append_figure = False,
    ):
    
    """
    Purpose: Will plot the saved
    proofread information of a neuron

    Ex: 
    #trying on inhibitory
    segment_id,split_index = (864691134917559306,0)

    original_mesh = du.fetch_segment_id_mesh(segment_id)

    pv.plot_proofread_neuron(
        segment_id,
        split_index,
        original_mesh=original_mesh,
        plot_error_mesh=False,
        verbose = True)


    """
    if not append_figure:
        import ipyvolume as ipv
        ipv.clear()
    
    su.ignore_warnings()
    
    if type(segment_id) == str:
        segment_id,split_index = pv.segment_id_and_split_index_from_node_name(segment_id)
    
    if verbose:
        print(f"Plotting {segment_id}_{split_index} (nucleus_id={pv.nucleus_id_from_segment_id(segment_id,split_index)})")
    
    if cell_type is None:
        cell_type = pv.cell_type_from_segment_id(segment_id,split_index)
        if verbose:
            print(f"cell_type = {cell_type}")
    if synapse_compartments is None:
        synapse_compartments = apu.compartments_to_plot(cell_type)
        
    if synapse_spine_bouton_labels is None:
        synapse_spine_bouton_labels = spu.spine_bouton_labels_to_plot()
        
    if compartments is None:
        compartments = apu.compartments_to_plot(cell_type)

    meshes = []
    meshes_colors = []
    skeletons = []
    skeletons_colors = []
    meshes_alpha = []
    
    
    if scatters is not None:
        scatters_colors = nu.convert_to_array_like(scatters_colors)
        if len(scatters_colors) == 1:
            scatters_colors = scatters_colors*len(scatters)
        scatter_sizes = nu.convert_to_array_like(scatter_sizes)
        if len(scatter_sizes) == 1:
            scatter_sizes = scatter_sizes*len(scatters)
    else:
        scatters = []
        scatters_colors = []
        scatter_sizes = []

    if original_mesh is None:
        original_mesh = du.fetch_segment_id_mesh(segment_id)

    compartment_color_dict = dict(valid_mesh=proofread_mesh_color)
        
    proof_mesh,error_mesh = pv.fetch_proofread_mesh(segment_id,
                            split_index = split_index,
                            original_mesh=original_mesh,
                            return_error_mesh=True)

    if plot_proofread_skeleton:
        proof_skeleton = pv.fetch_proofread_skeleton(segment_id,
                                   split_index,
                                   plot_skeleton=False,
                                   #original_mesh=original_mesh
                                                    )
    else:
        proof_skeleton = None

    if plot_error_mesh:
        meshes.append(error_mesh)
        meshes_colors.append(error_mesh_color)
        meshes_alpha.append(error_mesh_alpha)
        compartment_color_dict["error_mesh"] = error_mesh_color
    
    


    if plot_nucleus:
        nuc_center = pv.nucleus_center_from_segment_id(segment_id,
                                         split_index)
        
        if nucleus_color == "proofread_mesh_color":
            nucleus_color = proofread_mesh_color
        
        if nuc_center is None:
            print(f"No nucleus to plot")
        else:
            scatters += [nuc_center.reshape(-1,3)]
            scatters_colors += [nucleus_color]
            scatter_sizes += [nucleus_size]


    #get the synapse groups
    if plot_synapses:
    
        synapses_objs = pv.syanpse_objs_from_segment_id(segment_id,split_index)
        
        (syn_scatters,
        syn_colors,
        syn_sizes) = syu.synapse_plot_items_by_type_or_query(
                        synapses_objs,
                        synapses_size = synapses_size,
                        synapse_plot_type = synapse_plot_type,#"compartment"#  "valid_error"
                        synapse_compartments = synapse_compartments,
                        synapse_spine_bouton_labels = synapse_spine_bouton_labels,
                        plot_error_synapses = plot_error_synapses,
                        valid_synapses_color = valid_synapses_color,
                        error_synapses_color = error_synapses_color,
                        synapse_queries = synapse_queries,
                        synapse_queries_colors = synapse_queries_colors,
        
                        verbose = verbose,
                        print_spine_colors = print_spine_colors)

        scatters += syn_scatters
        scatters_colors += syn_colors
        scatter_sizes += syn_sizes
        
   

    if compartments is not None and len(compartments) > 0:
        comp_colors = apu.colors_from_compartments(compartments)
        if plot_compartment_meshes:
            comp_meshes = pv.fetch_compartments_meshes(compartments,
                                                      segment_id,
                                                     split_index,
                                                       original_mesh = original_mesh,
                                                     )
            meshes += comp_meshes
            meshes_colors += comp_colors
            meshes_alpha += [compartment_mesh_alpha]*len(comp_meshes)

        if plot_compartment_skeletons:
            comp_sk = pv.fetch_compartments_skeletons(compartments,
                                                      segment_id,
                                                     split_index,
                                                     )

            skeletons += comp_sk
            skeletons_colors += comp_colors
            
        compartment_color_dict.update({k:v for k,v in zip(compartments,comp_colors)})

    if print_compartment_colors:
        print(f"\nCompartment Colors:")
        for k,v in compartment_color_dict.items():
            print(f"  {k}:{v}")

    
#     print(f"proof_mesh== {proof_mesh}")
#     print(f"proof_skeleton = {proof_skeleton}")
#     if len(proof_skeleton) == 0:
#         proof_skeleton = None
#     print(f"skeletons = {skeletons}")
#     print(f"meshes = {meshes}")
#     print(f"scatters = {scatters}")
    nviz.plot_objects(main_mesh = proof_mesh,
                      main_mesh_alpha=proofread_mesh_alpha,
                      main_mesh_color=proofread_mesh_color,

                      main_skeleton=proof_skeleton,
                      main_skeleton_color=proofread_skeleton_color,

                     skeletons=skeletons,
                     skeletons_colors=skeletons_colors,

                     meshes=meshes,
                     meshes_colors=meshes_colors,
                     mesh_alpha=meshes_alpha,

                     scatters=scatters,
                     scatter_size=scatter_sizes,
                     scatters_colors=scatters_colors,
                      
                     show_at_end = show_at_end,
                    append_figure = append_figure,
                     )