<filename>rrsg_cgreco/_helper_fun/multislice_viewer.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 15 16:55:39 2017

@author: omaier

Copyright 2019 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import matplotlib.pyplot as plt
import numpy as np


###############################################################################
# 3D viewer  ##################################################################
###############################################################################
def imshow(volume, vmin=None, vmax=None):
    if volume.ndim <= 3:
        fig, ax = plt.subplots()
        ax.volume = volume
        ax.index = volume.shape[0] // 2
        ax.imshow(volume[ax.index], vmin=vmin, vmax=vmax)
        ax.axis('off')
        plt.title("Slice "+str(volume.shape[0] // 2))

    elif volume.ndim == 4:
        fig, ax = plt.subplots(int(np.ceil(np.sqrt(volume.shape[0]))),
                               int(np.ceil(np.sqrt(volume.shape[0]))))
        fig.tight_layout()
        fig.subplots_adjust(hspace=0, wspace=0)
        ax = ax.flatten()
        ni = int(np.ceil(np.sqrt(volume.shape[0])))
        nj = int(np.ceil(np.sqrt(volume.shape[0])))
        for j in range(nj):
            for i in range(ni):
                ax[i+ni*j].axis('off')
                if i+ni*j >= volume.shape[0]:
                    ax[i+ni*j].volume = np.zeros_like(volume[0])
                else:
                    ax[i+ni*j].volume = volume[i+(j*ni)]
                    ax[i+ni*j].index = volume[i+(j*ni)].shape[0] // 2
                    ax[i+ni*j].imshow(volume[i+(j*ni), ax[i+ni*j].index],
                                      vmin=vmin, vmax=vmax)
    else:
        raise NameError('Unsupported Dimensions')
    fig.canvas.mpl_connect('scroll_event', process_scroll)


def process_scroll(event):
    fig = event.canvas.figure
    ax = fig.axes
    for i in range(len(ax)):
        volume = ax[i].volume
        if int((ax[i].index - event.step) >= volume.shape[0] or
           int((ax[i].index - event.step) < 0)):
            pass
        else:
            ax[i].index = int((ax[i].index - event.step) % volume.shape[0])
            ax[i].images[0].set_array(volume[ax[i].index])
            plt.title("Slice "+str(ax[i].index))
            fig.canvas.draw()

<filename>rrsg_cgreco/_helper_fun/multislice_viewer.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 15 16:55:39 2017

@author: omaier

Copyright 2019 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import matplotlib.pyplot as plt
import numpy as np


###############################################################################
# 3D viewer  ##################################################################
###############################################################################
def imshow(volume, vmin=None, vmax=None):
    if volume.ndim <= 3:
        fig, ax = plt.subplots()
        ax.volume = volume
        ax.index = volume.shape[0] // 2
        ax.imshow(volume[ax.index], vmin=vmin, vmax=vmax)
        ax.axis('off')
        plt.title("Slice "+str(volume.shape[0] // 2))

    elif volume.ndim == 4:
        fig, ax = plt.subplots(int(np.ceil(np.sqrt(volume.shape[0]))),
                               int(np.ceil(np.sqrt(volume.shape[0]))))
        fig.tight_layout()
        fig.subplots_adjust(hspace=0, wspace=0)
        ax = ax.flatten()
        ni = int(np.ceil(np.sqrt(volume.shape[0])))
        nj = int(np.ceil(np.sqrt(volume.shape[0])))
        for j in range(nj):
            for i in range(ni):
                ax[i+ni*j].axis('off')
                if i+ni*j >= volume.shape[0]:
                    ax[i+ni*j].volume = np.zeros_like(volume[0])
                else:
                    ax[i+ni*j].volume = volume[i+(j*ni)]
                    ax[i+ni*j].index = volume[i+(j*ni)].shape[0] // 2
                    ax[i+ni*j].imshow(volume[i+(j*ni), ax[i+ni*j].index],
                                      vmin=vmin, vmax=vmax)
    else:
        raise NameError('Unsupported Dimensions')
    fig.canvas.mpl_connect('scroll_event', process_scroll)


def process_scroll(event):
    fig = event.canvas.figure
    ax = fig.axes
    for i in range(len(ax)):
        volume = ax[i].volume
        if int((ax[i].index - event.step) >= volume.shape[0] or
           int((ax[i].index - event.step) < 0)):
            pass
        else:
            ax[i].index = int((ax[i].index - event.step) % volume.shape[0])
            ax[i].images[0].set_array(volume[ax[i].index])
            plt.title("Slice "+str(ax[i].index))
            fig.canvas.draw()

#!/usr/bin/env python3 # -*- coding: utf-8 -*- Created on Wed Nov 15 16:55:39 2017  @author: omaier  Copyright 2019 <NAME>  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at      http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ############################################################################### # 3D viewer  ################################################################## ###############################################################################