.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/04_demos/viz_network.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_04_demos_viz_network.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_04_demos_viz_network.py:


=======================================================
Visualize Interdisciplinary map of the journals network
=======================================================

The goal of this app is to show an overview of the journals network structure
as a complex network. Each journal is shown as a node and their connections
indicates a citation between two of them.

.. GENERATED FROM PYTHON SOURCE LINES 12-13

First, let's import some useful functions

.. GENERATED FROM PYTHON SOURCE LINES 13-20

.. code-block:: Python


    from os.path import join as pjoin

    import numpy as np

    import fury


.. GENERATED FROM PYTHON SOURCE LINES 21-22

Then let's download some available datasets.

.. GENERATED FROM PYTHON SOURCE LINES 22-26

.. code-block:: Python


    files, folder = fury.data.fetch_viz_wiki_nw()
    categories_file, edges_file, positions_file = sorted(files.keys())


.. GENERATED FROM PYTHON SOURCE LINES 27-28

We read our datasets

.. GENERATED FROM PYTHON SOURCE LINES 28-33

.. code-block:: Python


    positions = np.loadtxt(pjoin(folder, positions_file))
    categories = np.loadtxt(pjoin(folder, categories_file), dtype=str)
    edges = np.loadtxt(pjoin(folder, edges_file), dtype=int)


.. GENERATED FROM PYTHON SOURCE LINES 34-36

We attribute a color to each category of our dataset which correspond to our
nodes colors.

.. GENERATED FROM PYTHON SOURCE LINES 36-45

.. code-block:: Python


    category2index = {category: i for i, category in enumerate(np.unique(categories))}

    index2category = np.unique(categories)

    categoryColors = fury.colormap.distinguishable_colormap(nb_colors=len(index2category))

    colors = np.array([categoryColors[category2index[category]] for category in categories])


.. GENERATED FROM PYTHON SOURCE LINES 46-47

We define our node size

.. GENERATED FROM PYTHON SOURCE LINES 47-50

.. code-block:: Python


    radii = 1 + np.random.rand(len(positions))


.. GENERATED FROM PYTHON SOURCE LINES 51-54

Lets create our edges now. They will indicate a citation between two nodes.
OF course, the colors of each edges will be an interpolation between the two
node that it connects.

.. GENERATED FROM PYTHON SOURCE LINES 54-64

.. code-block:: Python


    edgesPositions = []
    edgesColors = []
    for source, target in edges:
        edgesPositions.append(np.array([positions[source], positions[target]]))
        edgesColors.append(np.array([colors[source], colors[target]]))

    edgesPositions = np.array(edgesPositions)
    edgesColors = np.average(np.array(edgesColors), axis=1)


.. GENERATED FROM PYTHON SOURCE LINES 65-68

Our data preparation is ready, it is time to visualize them all. We start to
build 2 actors that we represent our data : sphere_actor for the nodes and
lines_actor for the edges.

.. GENERATED FROM PYTHON SOURCE LINES 68-83

.. code-block:: Python


    sphere_actor = fury.actor.sphere(
        centers=positions,
        colors=colors,
        radii=radii * 0.5,
        theta=8,
        phi=8,
    )

    lines_actor = fury.actor.line(
        edgesPositions,
        colors=edgesColors,
        opacity=0.1,
    )


.. GENERATED FROM PYTHON SOURCE LINES 84-86

All actors need to be added in a scene, so we build one and add our
lines_actor and sphere_actor.

.. GENERATED FROM PYTHON SOURCE LINES 86-92

.. code-block:: Python


    scene = fury.window.Scene()

    scene.add(lines_actor)
    scene.add(sphere_actor)


.. GENERATED FROM PYTHON SOURCE LINES 93-95

The final step ! Visualize and save the result of our creation! Please,
switch interactive variable to True if you want to visualize it.

.. GENERATED FROM PYTHON SOURCE LINES 95-103

.. code-block:: Python


    interactive = False

    if interactive:
        fury.window.show(scene=scene, size=(600, 600))

    fury.window.record(scene=scene, out_path="journal_networks.png", size=(600, 600))


.. GENERATED FROM PYTHON SOURCE LINES 104-106

This example can be improved by adding some interactivy with slider,
picking, etc. Play with it, improve it!


.. _sphx_glr_download_auto_examples_04_demos_viz_network.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: viz_network.ipynb <viz_network.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: viz_network.py <viz_network.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: viz_network.zip <viz_network.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_