{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Visualize Interdisciplinary map of the journals network\n\nThe goal of this app is to show an overview of the journals network structure\nas a complex network. Each journal is shown as a node and their connections\nindicates a citation between two of them.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, let's import some useful functions\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from os.path import join as pjoin\nfrom fury import colormap as cmap\nfrom fury.window import record\nimport numpy as np"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then let's download some available datasets.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from fury.data.fetcher import fetch_viz_wiki_nw\n\nfrom helios import NetworkDraw\nfrom helios.layouts.force_directed import HeliosFr\n\nfiles, folder = fetch_viz_wiki_nw()\ncategories_file, edges_file, positions_file = sorted(files.keys())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We read our datasets\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "positions = np.loadtxt(pjoin(folder, positions_file))\npositions = np.random.normal(scale=10, size=positions.shape)\ncategories = np.loadtxt(pjoin(folder, categories_file), dtype=str)\nedges = np.loadtxt(pjoin(folder, edges_file), dtype=int)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We attribute a color to each category of our dataset which correspond to our\nnodes colors.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "category2index = {category: i\n                  for i, category in enumerate(np.unique(categories))}\n\n\nindex2category = np.unique(categories)\n\ncategoryColors = cmap.distinguishable_colormap(nb_colors=len(index2category))\n\ncolors = np.array([categoryColors[category2index[category]]\n                   for category in categories])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We define our node size\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "radii = 1 + np.random.rand(len(positions))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Lets create our edges now. They will indicate a citation between two nodes.\nOF course, the colors of each edges will be an interpolation between the two\nnode that it connects.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "#edgesPositions = []\nedgesColors = []\nfor source, target in edges:\n    #edgesPositions.append(np.array([positions[source], positions[target]]))\n    edgesColors.append(np.array([colors[source], colors[target]]))\n\n#edgesPositions = np.array(edgesPositions)\nedgesColors = np.average(np.array(edgesColors), axis=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Our data preparation is ready, it is time to visualize them all. We start to\nbuild 2 actors that we represent our data : sphere_actor for the nodes and\nlines_actor for the edges.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "network_draw = NetworkDraw(\n        positions=positions,\n        colors=colors,\n        scales=4,\n        node_edge_width=0,\n        edge_line_color=edgesColors,\n        marker='3d',\n        edges=edges,\n)\nlayout = HeliosFr(edges, network_draw, update_interval_workers=10)\n\nlayout.start()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The final step ! Visualize and save the result of our creation! Please,\nswitch interactive variable to True if you want to visualize it.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "interactive = False \nif not interactive:\n    import time\n    time.sleep(10)\n    layout.stop()\n\nif interactive:\n    network_draw.showm.initialize()\n    network_draw.showm.start()\n\nrecord(\n    network_draw.showm.scene, out_path='viz_helios.png', size=(600, 600))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}