<commit_before>"""Plotting functions
- makeMTX: Generate 3D-matrix data
- visualize3D: Draw matrix data in 3D
"""
import numpy as _np
from collections import namedtuple

from plotly.offline import plot as pltoff
import plotly.graph_objs as go

from .process import pdc
from .sph import sph2cart

pi = _np.pi


def show(trace, colorize=False):
    data = [trace]
    if colorize:
        data.colorscale = 'Viridis'
        # data.surfacecolor = rs
    pltoff(data)


def makeMTX(Pnm, dn, Nviz=3, krIndex=1, oversize=1):
    """mtxData = makeMTX(Nviz=3, Pnm, dn, krIndex)

    Parameters
    ----------
    Pnm       Spatial Fourier Coefficients (from S/T/C)
    dn        Modal Radial Filters (from M/F)
    N         Order of the spatial fourier transform     [default = 3]
    krIndex   Index of kr Vector                         [default = 1]
    oversize  Integer Factor to increase the resolution. Set oversize = 1
              (default) to use the mtxData matrix for visual3D(), map3D().
    #Returns
    -------
    mtxData   3D-matrix-data in 1[deg] steps

    Notes
    -----
    The file generates a SOFiA mtxData Matrix of 181x360 pixels for the
    visualisation with visualize3D() in 1[deg] Steps (65160 plane waves).
    The HD version generally allows to raise the resolution (oversize > 1).
    (visual3D(), map3D() admit 1[deg] data only, oversize = 1)
    """
    if oversize < 1:
        raise ValueError('Oversize parameter must be >= 1')

    # Generate angles for sphere with 1[deg] spacing
    angles = _np.mgrid[0:360, 0:181].T.reshape((-1, 2)) * _np.pi / 180

    # Compute plane wave decomposition for all angles at given kr
    Y = pdc(Nviz, angles, Pnm[:, krIndex], dn[:, krIndex])

    return Y.reshape((181, -1))  # Return pwd data as [181, 360] matrix


def normalizeMTX(vizMTX):
    vizMTX -= vizMTX.min()
    return vizMTX / vizMTX.max()


def genSphCoords():
    """ Generates cartesian (x,y,z) and spherical (theta, phi) coordinates of a sphere
    Returns
    -------
    coords : named tuple
        holds cartesian (x,y,z) and spherical (theta, phi) coordinates
    """
    coords = namedtuple('coords', ['x', 'y', 'z', 'theta', 'phi'])
    theta = _np.linspace(0, 2 * pi, 360)
    phi = _np.linspace(0, pi, 181)
    coords.x = _np.outer(_np.cos(theta), _np.sin(phi))
    coords.y = _np.outer(_np.sin(theta), _np.sin(phi))
    coords.z = _np.outer(_np.ones(360), _np.cos(phi))

    coords.theta, coords.phi = _np.meshgrid(_np.linspace(0, _np.pi, 181), _np.linspace(0, 2 * _np.pi, 360))
    return coords


def sph2cartMTX(vizMTX):
    rs = vizMTX.reshape((181, -1)).T

    coords = genSphCoords()
    vizMTX = namedtuple('vizMTX', ['xs', 'ys', 'zs'])
    vizMTX.xs = rs * _np.sin(coords.theta) * _np.cos(coords.phi)
    vizMTX.ys = rs * _np.sin(coords.theta) * _np.sin(coords.phi)
    vizMTX.zs = rs * _np.cos(coords.theta)
    return vizMTX


def genShape(vizMTX, colorize=False):
    V = sph2cartMTX(vizMTX)

    trace = go.Surface(
        x=_np.abs(V.xs),
        y=_np.abs(V.ys),
        z=_np.abs(V.zs),
        showscale=False,
        hoverinfo='none'
    )
    return trace


def genSphere(vizMTX, colorize=False):
    #  visObj = scene.visuals.Sphere(radius=1, rows=362, cols=91, method='latitude', face_colors=colors)<commit_msg>disable monochrome plotting for now<commit_after>"""Plotting functions
- makeMTX: Generate 3D-matrix data
- visualize3D: Draw matrix data in 3D
"""
import numpy as _np
from collections import namedtuple

from plotly.offline import plot as pltoff
import plotly.graph_objs as go

from .process import pdc
from .sph import sph2cart

pi = _np.pi


def show(trace, colorize=True):
    data = [trace]

    # if colorize:
    #    data[0].autocolorscale = False
    #    data[0].surfacecolor = [0, 0.5, 1]
    pltoff(data)


def makeMTX(Pnm, dn, Nviz=3, krIndex=1, oversize=1):
    """mtxData = makeMTX(Nviz=3, Pnm, dn, krIndex)

    Parameters
    ----------
    Pnm       Spatial Fourier Coefficients (from S/T/C)
    dn        Modal Radial Filters (from M/F)
    N         Order of the spatial fourier transform     [default = 3]
    krIndex   Index of kr Vector                         [default = 1]
    oversize  Integer Factor to increase the resolution. Set oversize = 1
              (default) to use the mtxData matrix for visual3D(), map3D().
    #Returns
    -------
    mtxData   3D-matrix-data in 1[deg] steps

    Notes
    -----
    The file generates a SOFiA mtxData Matrix of 181x360 pixels for the
    visualisation with visualize3D() in 1[deg] Steps (65160 plane waves).
    The HD version generally allows to raise the resolution (oversize > 1).
    (visual3D(), map3D() admit 1[deg] data only, oversize = 1)
    """
    if oversize < 1:
        raise ValueError('Oversize parameter must be >= 1')

    # Generate angles for sphere with 1[deg] spacing
    angles = _np.mgrid[0:360, 0:181].T.reshape((-1, 2)) * _np.pi / 180

    # Compute plane wave decomposition for all angles at given kr
    Y = pdc(Nviz, angles, Pnm[:, krIndex], dn[:, krIndex])

    return Y.reshape((181, -1))  # Return pwd data as [181, 360] matrix


def normalizeMTX(vizMTX):
    vizMTX -= vizMTX.min()
    return vizMTX / vizMTX.max()


def genSphCoords():
    """ Generates cartesian (x,y,z) and spherical (theta, phi) coordinates of a sphere
    Returns
    -------
    coords : named tuple
        holds cartesian (x,y,z) and spherical (theta, phi) coordinates
    """
    coords = namedtuple('coords', ['x', 'y', 'z', 'theta', 'phi'])
    theta = _np.linspace(0, 2 * pi, 360)
    phi = _np.linspace(0, pi, 181)
    coords.x = _np.outer(_np.cos(theta), _np.sin(phi))
    coords.y = _np.outer(_np.sin(theta), _np.sin(phi))
    coords.z = _np.outer(_np.ones(360), _np.cos(phi))

    coords.theta, coords.phi = _np.meshgrid(_np.linspace(0, _np.pi, 181), _np.linspace(0, 2 * _np.pi, 360))
    return coords


def sph2cartMTX(vizMTX):
    rs = vizMTX.reshape((181, -1)).T

    coords = genSphCoords()
    vizMTX = namedtuple('vizMTX', ['xs', 'ys', 'zs'])
    vizMTX.xs = rs * _np.sin(coords.theta) * _np.cos(coords.phi)
    vizMTX.ys = rs * _np.sin(coords.theta) * _np.sin(coords.phi)
    vizMTX.zs = rs * _np.cos(coords.theta)
    return vizMTX


def genShape(vizMTX):
    V = sph2cartMTX(vizMTX)

    trace = go.Surface(
        x=_np.abs(V.xs),
        y=_np.abs(V.ys),
        z=_np.abs(V.zs),
        surfacecolor=_np.abs(vizMTX.reshape((181, -1))).T,
        colorscale='Viridis',
        showscale=False,
        hoverinfo='none'
    )
    return trace


def genSphere(vizMTX, colorize=False):
    #  visObj = scene.visuals.Sphere(radius=1, rows=362, cols=91, method='latitude', face_colors=colors)
{'fraction_non_alphanumeric': 0.13613193403298351, 'fraction_numerical': 0.02908545727136432, 'mean_word_length': 2.7710570944036177, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2041208', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2428, 'n_words': 653}